#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a struct SegmentTree{ // using H = function; Int n,height; H h; E ei; vector laz; SegmentTree(H h,E ei):h(h),ei(ei){} void init(Int n_){ n=1;height=0; while(n>i); } void update(Int a,Int b,E x){ if(a>=b) return; thrust(a+=n); thrust(b+=n-1); for(Int l=a,r=b+1;l>=1,r>>=1){ if(l&1) laz[l]=h(laz[l],x),l++; if(r&1) --r,laz[r]=h(laz[r],x); } } E get_val(Int a){ thrust(a+=n); return laz[a]; } void set_val(Int a,E x){ thrust(a+=n); laz[a]=x; } }; //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int n,m; cin>>n>>m; vector as(n); for(Int i=0;i>as[i]; vector xs(m),ws(m); for(Int i=0;i>xs[i]>>ws[i],xs[i]--; auto h=[&](Int a,Int b){return a+b;}; SegmentTree d1(h,0); SegmentTree d2(h,0); auto check= [&](Int c)->bool{ d1.init(n);d2.init(n); for(Int i=0;i=as[i]) return 0; return 1; }; const Int INF = 1e5+10; Int L=-1,R=INF; while(L+1>1; if(check(M)) R=M; else L=M; } if(R==INF) R=-1; cout<