#include #include using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)(n);i++) int main(){ int n,m; cin>>n>>m; vector v(n); rep(i,n) v.at(i)=i; rep(i,m){ int t; cin>>t; vector s(t); rep(i,t) cin>>s.at(i),s.at(i)--; int bf=v.at(s.at(0)); for(int i=t-1;i>=0;i--){ v.at(s.at((i+1)%t))=v.at(s.at(i)); } v.at(s.at(1))=bf; } atcoder::dsu uf(n); rep(i,n) uf.merge(i,v.at(i)); map mp; rep(i,n) mp[uf.leader(i)]++; ll ans=1; for(auto[i,sz]:mp){ ans=ans*sz/gcd(ans,sz); } cout<