#include using namespace std; using ll =long long; #define all(v) v.begin(),v.end() #define rep(i,a,b) for(int i=a;i=b;i--) ll INF=2e18; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N,Q;cin>>N>>Q; string S;cin>>S; vector x(N+1),y(N+1); for(ll i=0;i>H>>W>>P; //何回の移動? ll s=0; ll g=1000000000000000; while(s0) { X+=x[k]; Y+=y[k]; k=0; } if(X>=H||Y>=W) { g=K; } else s=K+1; } ll k=s; ll noko=N-P; ll X=0,Y=0; if(noko<=k) { X+=x[N]-x[P]; Y+=y[N]-y[P]; k-=N-P; } else { X+=x[P+k]-x[P]; Y+=y[P+k]-y[P]; k=0; } ll count=k/N; k%=N; X+=x[N]*count; Y+=y[N]*count; if(k>0) { X+=x[k]; Y+=y[k]; k=0; } cout<<(P+s)%N<