#include <bits/stdc++.h>
#define int long long
int b[30010];
int p[30010];
std::vector<std::pair<int,int>> adj[30010];
int dist[30010];
signed main() {
int n,m;
std::cin >> n >> m;
int s=0,e=1;
for(int i=0;i<m;i++){
std::cin >> b[i] >> p[i];
if(i==0)s=b[i];
else if(i==1)e=b[i];
}
for(int i=0;i<n;i++){
dist[i]=1e18;
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(std::abs(j-b[i])%p[i]==0){
adj[b[i]].push_back({j,std::abs(j-b[i])/p[i]});
}
}
}
for(int u=0;u<n;u++){
std::vector<int> best(n,1e18);
for(auto [v,w]:adj[u]){
if(w<best[v])best[v]=w;
}
std::vector<std::pair<int,int>> newAdj;
for(int v=0;v<n;v++){
if(best[v]!=1e18){
newAdj.push_back({v,best[v]});
}
}
adj[u]=newAdj;
}
std::priority_queue<std::pair<int,int>> pq;
pq.push({-0,s});
while(!pq.empty()){
int weight=-pq.top().first;
int node=pq.top().second;
pq.pop();
for(auto [to,w]:adj[node]){
if(weight+w<dist[to]){
dist[to]=weight+w;
pq.push({-(dist[to]),to});
}
}
}
if(dist[e]>=1e18)dist[e]=-1;
std::cout << dist[e];
}