#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 30001
const int inf = 1e18;
int tc = 1, n, B[N], P[N], m, dis[N], vis[N];
vector <int> E[N];
set <int> s;
int32_t main() {
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> B[i] >> P[i];
s.insert(B[i]);
E[B[i]].push_back(P[i]);
}
for(int i = 0; i < n; i++) {
dis[i] = inf;
}
priority_queue <tuple<int,int, int>> q;
for(auto i : E[B[0]]) {
q.push({0, B[0], i});
}
dis[B[0]] = 0;
while(!q.empty()) {
int pw;
int ind;
int cur;
tie(cur, ind, pw) = q.top();
q.pop();
if(dis[ind] != -cur) continue;
for(auto j : s) {
int ds = abs(j - ind);
if(ds % pw != 0 || dis[j] <= dis[ind] + (ds / pw)) continue;
dis[j] = dis[ind] + (ds / pw);
for(auto i : E[j]) {
q.push({-dis[j], j, i});
}
}
}
if(dis[B[1]] == inf) dis[B[1]] = -1;
cout << dis[B[1]] << '\n';
return 0;
}