#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,t[1005],x,y,color[1005],ans=1e18;
signed main(){
cin>>n>>k;
for(int i=1;i<=k;i++){
cin>>t[i]>>x>>y;
color[i]=(x+(i-1)*y)%n;
if(i==1) color[i]=x;
}
for(int i=1;i<k;i++){
for(int j=i+1;j<=k;j++){
if(color[i]!=color[j]) ans=min(ans,(t[i]*t[j]/__gcd(t[i],t[j])));
}
}
if(ans==1e18) cout<<"Mystia will cook forever...";
else cout<<ans-1;
return 0;
}