#include <algorithm>
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algos/debug.h"
#else
#define debug(...) 42;
#endif
#define all(x) (x).begin(), (x).end()
#define isz(x) (int)x.size()
#define int long long
const int sz = 1e6 + 1, inf = 1e18;
array<int, 2> a[sz];
void run(int tc) {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1];
}
vector<int> p(m);
iota(all(p), 0);
int ans = inf;
do {
if (p[0] != 0)
continue;
int cost = 0;
for (int i = 1; i < m; i++) {
if (p[i - 1] == 1) {
break;
}
if (abs(a[p[i]][0] - a[p[i - 1]][0]) % a[p[i - 1]][1]) {
cost = inf;
break;
}
cost += abs(a[p[i]][0] - a[p[i - 1]][0]) / a[p[i - 1]][1];
}
debug(p, cost);
ans = min(ans, cost);
} while (next_permutation(all(p)));
cout << (ans == inf ? -1 : ans) << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
for (int tc = 1; tc <= t; tc++)
run(tc);
}