#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
const int N=3e4+2;
bool vis[N];
vector<int>vc[N];
vector<pair<int,int>>v[N];
int X[N],P[N];
int dst[N][N];
signed main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int x,p;
cin>>x>>p;
X[i]=x;
P[i]=p;
p=abs(p);
for(int j=1;j<=n;j++){
if(x-p*j>=0)v[x].pb({x-p*j,j});
if(x+p*j<n)v[x].pb({x+p*j,j});
}
vc[x].pb(p);
}
dst[X[0]][P[0]]=1;
queue<pair<int,int>>q ;
q.push({X[0],P[0]});
while(!q.empty()){
auto [x,p]=q.front();
q.pop();
if(x+p<n){
for(auto a:vc[x+p]){
if(dst[x+p][a]==0){
dst[x+p][a]=dst[x][p]+1;
q.push({x+p,a});
}
}
if(dst[x+p][p]==0){
dst[x+p][p]=dst[x][p]+1;
q.push({x+p,p});
}
}
if(x-p>=0 and dst[x-p][p]==0){
for(auto a:vc[x-p]){
if(dst[x-p][a]==0){
dst[x-p][a]=dst[x][p]+1;
q.push({x-p,a});
}
}
if(dst[x-p][p]==0){
dst[x-p][p]=dst[x][p]+1;
q.push({x-p,p});
}
}
}
int ans=1e9;
for(int i=0;i<N;i++){
if(dst[X[1]][i])ans=min(ans,dst[X[1]][i]);
}
if(ans==1e9)ans=-1;
cout<<ans-1;
}
Compilation message (stderr)
/tmp/cchkCbep.o: in function `__tcf_0':
skyscraper.cpp:(.text+0x9): relocation truncated to fit: R_X86_64_PC32 against symbol `vc' defined in .bss section in /tmp/cchkCbep.o
/tmp/cchkCbep.o: in function `__tcf_1':
skyscraper.cpp:(.text+0x59): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/cchkCbep.o
/tmp/cchkCbep.o: in function `main':
skyscraper.cpp:(.text.startup+0x10): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a(globals_io.o)
skyscraper.cpp:(.text.startup+0x67): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x91): relocation truncated to fit: R_X86_64_PC32 against symbol `X' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0xa1): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x16e): relocation truncated to fit: R_X86_64_PC32 against symbol `vc' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x1a9): relocation truncated to fit: R_X86_64_PC32 against symbol `X' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x1b0): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x276): relocation truncated to fit: R_X86_64_PC32 against symbol `X' defined in .bss section in /tmp/cchkCbep.o
skyscraper.cpp:(.text.startup+0x288): additional relocation overflows omitted from the output
/usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a(ios_init.o): in function `std::ios_base::Init::Init()':
(.text._ZNSt8ios_base4InitC2Ev+0x1c): failed to convert GOTPCREL relocation against '_ZNSt8ios_base4Init11_S_refcountE'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x1c6): failed to convert GOTPCREL relocation against '_ZSt4cout'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x260): failed to convert GOTPCREL relocation against '_ZSt3cin'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x2e2): failed to convert GOTPCREL relocation against '_ZSt4cerr'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x353): failed to convert GOTPCREL relocation against '_ZSt4clog'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x541): failed to convert GOTPCREL relocation against '_ZSt5wcout'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x5e5): failed to convert GOTPCREL relocation against '_ZSt4wcin'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x670): failed to convert GOTPCREL relocation against '_ZSt5wcerr'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x6e9): failed to convert GOTPCREL relocation against '_ZSt5wclog'; relink with --no-relax
/usr/bin/ld: final link failed
collect2: error: ld returned 1 exit status