#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;
vector<int>g[2000 + 1];
bool al[2001];
bool was[2001];
vector<int>pt;
int fin;
void dfs(int v) {
    was[v] = 1;
    if (v == fin) {
        al[pt[0]] = 1;
        al[pt.back()] = 1;
    }
    for (auto u : g[v]) {
        if (!was[u]) {
            pt.push_back(v);
            dfs(u);
            pt.pop_back();
        }
    }
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
        g[i].push_back(i + 1);
    }
    g[n].push_back(1);
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        if (!(u == n && v == 1) && !(v == u + 1)) {
            fill(was + 1, was + n + 1, 0);
            fin = v;
            dfs(u);
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!al[i]) {
            ans++;
        }
    }
    cout << ans << endl << -1 << endl;
}
/*
A:12
B:100
C:27
D:


*/