#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,bmi,bmi2,popcnt,lzcnt,tune=native")
using namespace std;
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    bool was[n + 1] = {};
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        if (!(u == n && v == 1) && !(v == u + 1)) {
            if (u < v) {
                for (int i = u; i < v; i++) {
                    was[i] = 1;
                }
            } else {
                for (int i = v; i < n; i++) {
                    was[i] = 1;
                }
                for (int i = 1; i < u; i++) {
                    was[i] = 1;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1; i < n; i++) {
        if (!was[i]) {
            ans++;
        }
    }
    cout << ans << endl << -1 << endl;
}
/*
A:12
B:100
C:27
D:


*/