#include <bits/stdc++.h>
#define int long long
int b[30010];
int p[30010];
int adj[2015][2015];
int dist[2015];
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]=1e9;
for(int j=0;j<n;j++){
adj[i][j]=1e9;
}
}
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]][j]=std::min(adj[b[i]][j],std::abs(j-b[i])/p[i]);
}
}
}
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(int j=0;j<n;j++){
if(weight+adj[node][j]<dist[j]){
dist[j]=weight+adj[node][j];
pq.push({-(weight+adj[node][j]),j});
}
}
}
if(dist[e]>=1e9)dist[e]=-1;
std::cout << dist[e];
}