#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++){
std::map<int,int> best;
for(int j=b[i];j<n;j+=p[i]){
int w=(j-b[i])/p[i];
if(!best.count(j)||w<best[j])best[j]=w;
}
for(int j=b[i]-p[i];j>=0;j-=p[i]){
int w=(b[i]-j)/p[i];
if(!best.count(j)||w<best[j])best[j]=w;
}
for(auto &it:best){
adj[b[i]].push_back({it.first,it.second});
}
}
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];
}