#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k,ans,x=1;
    cin>>n>>k;
    for(int i =0;i < k; i++)
    {
        bool f = 0;
        int m;  cin>>m;
        int last;   cin>>last;
        if(last==1) f=1;
        for(int i =1; i < m; i++)
        {
            int curr;   cin>>curr;
            x+= f && (curr==1+last);
            if(curr!=1+last)    f = 0;
            last = curr;
        }
    }
    cout<<2*(n-x)-k+1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    int n,k;
    cin>>n>>k;
    if(k==1)
    {
        cout<<0;
        return 0;
    }
    int timeToClose = 0;
    int timeToBreak = 0;
    for(int i =0;i < k; i++)
    {
        bool first = false;
        int m;
        cin>>m;
        int last;
        cin>>last;

        if(last==1)
            first=true;

        if(m==1&&!first)
        {
            timeToClose++;
            timeToBreak++;
        }
        else if(m==1 && first)
            timeToClose++;
        for(int j =1;j < m; j++)
        {
            int curr;
            cin>>curr;

            if(curr>last+1)
            {
                if(first)
                    timeToClose += m-j;
                else
                    timeToClose += m;
                timeToBreak += m-j;
                for(int k = 0; k < m-j-1; k++)
                    cin>>curr;
                break;
            }
            if(j==m-1 && !first)
            {
                timeToBreak+=j;
                timeToClose+=j;
            }
            last = curr;
        }
    }
    cout<<timeToBreak+timeToClose;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    int n,k;
    cin>>n>>k;
    int timeToClose = 0;
    int timeToBreak = 0;
    for(int i =0;i < k; i++)
    {
        bool first = false;
        int m;
        cin>>m;
        int last;
        cin>>last;

        if(last==1)
            first=true;

        if(m==1)
            timeToClose++;
        for(int j =1;j < m; j++)
        {
            int curr;
            cin>>curr;

            if(curr>last+1)
            {
                if(first)
                    timeToClose += m-j;
                else
                    timeToClose += m;
                timeToBreak += m-j;
                for(int k = 0; k < m-j-1; k++)
                    cin>>curr;
                break;
            }
            if(j==m-1 && !first)
                timeToClose+=j;
            last = curr;
        }
    }
    cout<<timeToBreak+timeToClose;
    return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <int> a[2000001];struct o{int x,y;};
 vector <o> tt;
int main(){
 //  Freopen;
 cin>>n>>m;
 int op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[k].push_back(x);
              if (a[k][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){
                //   cout<<t<<' '<<a[k][i-2];
                   op+=a[k][i-2]-pre+1;o w;w.x=pre;w.y=a[k][i-2];tt.push_back(w);pre=x;}
              if (i==t){
                        o w;
                        tt.push_back(w);
                        }
              }

     }
     op+=tt.size()-1;
     cout<<op;
return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <int> a[2000001];struct o{int x,y;};
 vector <o> tt;
 bool f (o a,o b){
      return a.y<b.y;
      }
int main(){
//  Freopen;
 cin>>n>>m;
 int op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[k].push_back(x);
              if (a[k][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){
                //   cout<<t<<' '<<a[k][i-2];
                   op+=a[k][i-2]-pre+1;o w;w.x=pre;w.y=a[k][i-2];tt.push_back(w);pre=x;}
              if (i==t){
                        o w;w.y=x;w.x=pre;
                        tt.push_back(w);
                        }
              }

     }
     sort(tt.begin(),tt.end(),f);
     op+=tt.size()-1;
     for (int k=tt.size()-1;k>0;--k){
         if (k!=1){
                   op+=(tt[k].y-tt[k].x)*2;
                   }
     }
     cout<<op;
return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <int> a[2000001];struct o{int x,y;};
 vector <o> tt;
int main(){
 //  Freopen;
 cin>>n>>m;
 int op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[k].push_back(x);
              if (a[k][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){
                //   cout<<t<<' '<<a[k][i-2];
                   ++op;o w;w.x=pre;w.y=a[k][i-2];tt.push_back(w);pre=x;}
              if (i==t){o w;w.x=x;w.y=x;
                        tt.push_back(w);
                       }
              }
     }
     op+=tt.size()-1;
     cout<<op;
return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <int> a[2000001];struct o{int x,y;};
 vector <o> tt;
int main(){
 //  Freopen;
 cin>>n>>m;
 int op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[t].push_back(x);
              if (a[t][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){++op;o w;w.x=pre;w.y=a[t][i-2];tt.push_back(w);pre=x;}
              if (i==t){o w;w.x=x;w.y=x;
                        tt.push_back(w);
                       }
              }
     }
     op+=tt.size()-1;
     cout<<op;
return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <int> a[2000001];struct o{int x,y;};
 vector <o> tt;
 bool f (o a,o b){
      return a.y<b.y;
      }
int main(){
// Freopen;
 cin>>n>>m;
 int op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[k].push_back(x);
              if (a[k][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){
                //   cout<<t<<' '<<a[k][i-2];
                   op+=a[k][i-2]-pre+1;o w;w.x=pre;w.y=a[k][i-2];tt.push_back(w);pre=x;}
              if (i==t){
                        o w;w.y=x;w.x=pre;
                        tt.push_back(w);
                        }
              }

     }
     sort(tt.begin(),tt.end(),f);
     op+=tt.size()-1;
     for (int k=tt.size()-1;k>0;--k){
         if (k!=0){
                   op+=(tt[k].y-tt[k].x)*2;
                   }
     }
     cout<<op;
return 0;
}


 // i see in the future and you are not in it :))
// idzite na xui vseee :))
 #define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <math.h>
#include<set>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
#include<stdio.h>
#include <queue>
#include <utility>
#include <string>
#define cn(x) scanf("%d",&x);
#define INF 21403647
#define PI 3.14159265358
#define mod9 1000000009
#define mod7 %1000000007
#define md 1000000007
#define f first
#define s second
#define rtn return 0
#define time clock()/(double)CLOCKS_PER_SEC
#define Freopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define maxn 500000
#define ct(n) printf("%d\n",n);
#define ctt(n) printf("%d\n",n);
#define cantinue continue
# define ll long long
# define pp pair <int,int>
using namespace std;int n,aa,bb,m;
 vector <ll> a[2000001];struct o{ll x,y;};
 vector <o> tt;
 bool f (o a,o b){
      return a.y<b.y;
      }
int main(){
 //Freopen;
 cin>>n>>m;
 ll op=0;
 for (int k=1;k<=m;++k)
 {int t;
     cin>>t; int pre=0,ind=0;
     for (int i=1;i<=t;++i)
       {
              int x;
              cin>>x;
              if (i==1) pre=x;
              a[k].push_back(x);
              if (a[k][i-2]==x-1 && i!=1){
                               }
              else if (i!=1){
                //   cout<<t<<' '<<a[k][i-2];
                   if (!ind)
                   ind=i-1;
                   }}
           if (ind==0)
           {
                      o w;w.x=a[k][0];
                      w.y=a[k][a[k].size()-1];
                      tt.push_back(w);
                      }
                      else {
                           op+=a[k].size()-ind;
                      //     cout<<ind<<' '<<op<<endl;
                           o w;w.x=a[k][0];w.y=a[k][ind-1];
                           tt.push_back(w);
                           for (int i=ind;i<a[k].size();++i)
                             {
                                    o w;w.x=a[k][ind];w.y=w.x;
                                    tt.push_back(w);
                                    }
                           }
     }
     sort(tt.begin(),tt.end(),f);
     op+=tt.size()-1;
     for (int k=tt.size()-1;k>0;--k){
         if (k!=0){
                   op+=(tt[k].y-tt[k].x)*2;
                   }
     }
     cout<<op;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0) ;
    long long n, k, ng = 0, ans = 0, sep = 0 ;
    cin >> n >> k ;
    for(int i=0;i<k;i++)
    {
        long long x, m, x1 ;
        cin >> m ;
        bool still = 1 ;
        for(int j=0;j<m;j++)
        {
            if(j==0)
            {
                cin >> x ;
                x1 = x ;
            }else
            {
                cin >> x ;
                if(still&&x-x1!=1)
                {
                    ng++ ;
                    still = 0 ;
                }else if(!still)
                    ng++ ;
                x1 = x ;
            }
        }
        ans += ng ;
        sep += ng+1 ;
        ng = 0 ;
    }
    //cout << ans << " " << g << endl ;
    ans += sep-1 ;
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0) ;
    long long n, k, ans = 0, m1 ;
    bool firstChain = 1, b = 0 ;
    cin >> n >> k ;
    for(int i=1;i<=k;i++)
    {
        long long m, x1 ;
        cin >> m ;
        for(int j=1;j<=m;j++)
        {
            long long x ;
            cin >> x ;
            if(j==1&&x==1)
            {
                b = 1 ;
                m1 = m ;
                long long x1 = x ;
                for(int temp=2;temp<=m;temp++)
                {
                    long long jj ;
                    cin >> jj ;
                    if(jj-x1!=1)
                        firstChain = 0 ;
                    x1 = jj ;
                }
                break ;
            }
        }
        if(b&&!firstChain)
            ans += m-1 ;
        else if(!b)
            ans += m-1 ;
        b = 0 ;
    }
    if(firstChain)
        ans += n-m1 ;
    else
        ans += n-1 ;
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0) ;
    long long n, k, g = 0, ans = 0 ;
    cin >> n >> k ;
    for(int i=0;i<k;i++)
    {
        int x, m, x1 ;
        cin >> m ;
        bool isG = 1 ;
        for(int j=0;j<m;j++)
        {
            if(j==0)
            {
                cin >> x ;
                x1 = x ;
            }else
            {
                cin >> x ;
                if(x-x1!=1)
                    isG = 0 ;
                x1 = x ;
            }
        }
        if(isG)
            g += m ;
        else
            ans += m-1 ;
    }
    //cout << ans << " " << g << endl ;
    if(g==0)
        ans += n-1 ;
    else if(g==n)
        ans += k-1 ;
    else
        ans += n-g ;
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0) ;
    long long n, k, ans = 0, m1 = 0 ;
    bool firstChain = 1, b = 0 ;
    cin >> n >> k ;
    for(int i=1;i<=k;i++)
    {
        long long m, x1 ;
        cin >> m ;
        for(int j=1;j<=m;j++)
        {
            long long x ;
            cin >> x ;
            if(j==1&&x==1)
            {
                b = 1 ;
                long long x1 = x ;
                for(int temp=2;temp<=m;temp++)
                {
                    long long jj ;
                    cin >> jj ;
                    if(jj-x1!=1)
                        firstChain = 0 ;
                    if(!firstChain)
                        ans++ ;
                    else if(firstChain)
                        m1++ ;
                    x1 = jj ;
                }
                break ;
            }
        }
        if(!b)
            ans += m-1 ;
        b = 0 ;
    }
    if(m1==0)
        ans += n-1 ;
    else
        ans += n-(m1+1) ;
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0) ;
    long long n, k, ng = 0, ans = 0, sep = 0 ;
    cin >> n >> k ;
    for(int i=0;i<k;i++)
    {
        long long x, m, x1 ;
        cin >> m ;
        for(int j=0;j<m;j++)
        {
            if(j==0)
            {
                cin >> x ;
                x1 = x ;
            }else
            {
                cin >> x ;
                if(x-x1!=1)
                    ng++ ;
                x1 = x ;
            }
        }
        ans += ng ;
        sep += ng+1 ;
        ng = 0 ;
    }
    //cout << ans << " " << g << endl ;
    ans += sep-1 ;
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                ls++;
            }
            else{
                res += mat[i].size()-1;
                break;
            }
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0, idx = -1;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                idx = i;
                ls++;
            }
            else if(mat[i][j+1]-mat[i][j] != 1){
                res++;
            }
        }
    }

    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0, idx = -1;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][j+1]-mat[i][j] != 1){
                res += mat[i].size()-j-1;
                break;
            }
        }
    }

    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][j+1]-mat[i][j] > 1){
                res += mat[i].size()-1;
                break;
            }
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                ls++;
            }
            else{
                res += mat[i].size()-1-j;
                break;
            }
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][j+1]-mat[i][j] > 1) res++;
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0, idx = -1;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                idx = i;
                ls++;
            }
            else{
                res++;
            }
        }
    }
    if(ls == mat[idx].size()) --k;
    if(!k) k++;
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                ls++;
            }
            else if(mat[i][0] == 1){
                res++;
            }
            else res += mat[i].size() -1;
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0, idx = -1;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                idx = i;
                ls++;
            }
            else if(mat[i][j+1]-mat[i][j] != 1){
                res += mat[i].size()-j-1;
                break;
            }
        }
    }

    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0, idx = -1;

    lp(i,0,k){
        int flag = 0;
        for(int j = mat[i].size()-1; j > 0; j--){
            if((mat[i][j] - mat[i][j-1]) != 1){
                if(!flag) res += mat[i].size()-j;
                else res++;
                flag = 1;
            }
        }
    }

    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


#include <bits/stdc++.h>

#define printv(i,v) for(int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << "\n"
#define lp(i,s,n) for(int i = s; i < n; i++)
#define all(v) (v).begin(),(v).end()
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vb vector<bool>
#define vs vector<string>
#define INF int(1e8)
#define NINF int(-1e8)
#define clr(a,v) memset(a,v,sizeof(a))
#define ll long long
#define ld long double

using namespace std;

int n,k;
vvi mat;

int main()
{
    cin >> n >> k;
    lp(i,0,k){
        int m; cin >> m;
        vi tmp;
        lp(j,0,m){
            int m1; cin >> m1;
            tmp.push_back(m1);
        }
        mat.push_back(tmp);
    }
    ll res = 0, ls = 0;

    lp(i,0,k){
        lp(j,0,mat[i].size()-1){
            if(mat[i][0] == 1 && (mat[i][j+1]-mat[i][j]) == 1){
                ls++;
            }
            else{
                res++;
            }
        }
    }
    res = (res*2) + (k-1);

    cout << res << "\n";

    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m[mx+5],x,count,ans;
    vi chain[mx+5];
    cin>>n>>k;
    count=1;
    ans=n-1;
    for(i=0; i<k; i++)
    {
        cin>>m[i];
        ans+=m[i]-1;
        //pr1(ans);
        for(j=0; j<m[i]; j++)
        {
            cin>>x;
            chain[i].pb(x);
            if(j && chain[i][0]==1 && chain[i][j-1]+1==chain[i][j])
                count++;
        }
        if(chain[i][0]==1 && count!=m[i])
            count=0;
    }
    if(count)
        ans-=count;
    ans=max(ans,0);
    pr1(ans);
    //pr1(count);
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m,x,ans;
    cin>>n>>k;
    ans=2*n-k+1;
    for(i=0; i<k; i++)
    {
        cin>>m;
        for(j=0; j<m; j++)
        {
            cin>>x;
            if(j+1==x)
                ans-=2;
        }
    }
    pr1(ans);
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m[mx+5],x,count,ans;
    vi chain[mx+5];
    cin>>n>>k;
    count=1;
    ans=n-1;
    for(i=0; i<k; i++)
    {
        cin>>m[i];
        ans+=m[i]-1;
        //pr1(ans);
        for(j=0; j<m[i]; j++)
        {
            cin>>x;
            chain[i].pb(x);
            if(j && chain[i][0]==1 && chain[i][j-1]+1==chain[i][j])
                count++;
        }
        if(chain[i][0]==1 && count!=m[i])
            count=0;
    }
    if(count)
        ans-=count;
    pr1(ans);
    //pr1(count);
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m,x,ans;
    scanf("%d %d",&n,&k);
    ans=2*n-k+1;
    for(i=0; i<k; i++)
    {
        scanf("%d",&m);
        for(j=0; j<m; j++)
        {
            scanf("%d",&x);
            if(j+1==x)
                ans-=2;
        }
    }
    pr1(ans);
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m,x,count,ans;
    scanf("%d %d",&n,&k);
    count=0;
    ans=n-1;
    for(i=0; i<k; i++)
    {
        scanf("%d",&m);
        ans+=m-1;
        for(j=0; j<m; j++)
        {
            scanf("%d",&x);
            if(j+1==x)
                count++;
        }
    }
    ans-=2*(count-1);
    pr1(ans);

    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m,x,count,ans;
    cin>>n>>k;
    count=0;
    ans=n-1;
    for(i=0; i<k; i++)
    {
        cin>>m;
        ans+=m-1;
        //pr1(ans);
        for(j=0; j<m; j++)
        {
            cin>>x;
            if(j+1==x)
                count++;
        }
    }
    //pr1(count);
    ans-=2*(count-1);
    pr1(ans);

    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m[mx+5],x,count,ans;
    vi chain[mx+5];
    cin>>n>>k;
    count=0;
    ans=n-1;
    for(i=0; i<k; i++)
    {
        cin>>m[i];
        ans+=m[i]-1;
        //pr1(ans);
        for(j=0; j<m[i]; j++)
        {
            cin>>x;
            chain[i].pb(x);
            if(j && chain[i][0]==1 && chain[i][j-1]+1==chain[i][j])
                count++;
        }
        if(chain[i][0]==1 && count!=m[i]-1)
            count=0;
    }
    if(count)
        ans-=2*count;
    ans=max(ans,0);
    pr1(ans);
    //pr1(count);
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files included */

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          printf("\n")

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define tc1(x)      printf("Case %d: ",x)
#define tc2(x)      printf("Case #%d: ",x)
#define tc3(x)      printf("Case %d:\n",x)
#define tc4(x)      printf("Case #%d:\n",x)

#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"
/* defining macros */

using namespace std;

template <class T> inline T bigmod(T b, T p, T m)
{
    T ret;
    if(p==0) return 1;
    if(p&1)
    {
        ret=(bigmod(b,p/2,m)%m);
        return ((b%m)*ret*ret)%m;
    }
    else
    {
        ret=(bigmod(b,p/2,m)%m);
        return (ret*ret)%m;
    }
}
template <class T> inline T _sqrt(T a)
{
    return (T)sqrt((double)a);
}
template <class T, class X> inline T _pow(T a, X b)
{
    T res=1;
    for(int i=1; i<=b; i++)
        res*=a;
    return res;
}
/* template functions */

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef pair<double, double>pdd;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<double>vd;
/* type definition */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */

int tc=1;
const long long int mx=100000;
/* global declaration */

int main()
{
    int n,k,i,j,m,x,ans;
    vi chain[mx+5];
    cin>>n>>k;
    ans=2*n-k+1;
    for(i=0; i<k; i++)
    {
        cin>>m;
        //pr1(ans);
        for(j=0; j<m; j++)
        {
            cin>>x;
            chain[i].pb(x);
            if(j+1==chain[i][j])
                ans-=2;
        }
    }
    pr1(ans);
    //pr1(count);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn = 10005;
struct A{
	vector<int> val;
}chain[maxn];
int val[maxn];

int bs(int n, int key)
{
	int l=0, r=n-1;
	while (l<=r)
	{
		int m = (l+r)>>1;
		if (val[m] == key) return m;
		if (val[m] < key) l = m+1;
		else r = m-1;
	}
	return -1;
}

int main()
{
	int n, m, i, j, k, now, pre, ans, a;

	while (scanf ("%d%d", &n, &k) != EOF)
	{
		for (i=0, j=0; i<k; i++)
		{
			chain[i].val.clear();

			scanf ("%d", &m);
			while (m--)
			{
				scanf ("%d", &a);
				chain[i].val.push_back(a);
				val[j++] = a;
			}
		}
		sort(val, val+n);

		ans = n-1;
		for (i=0; i<k; i++)
		{
			for (j=0; j<chain[i].val.size(); j++)
			{
				now = bs(n, chain[i].val[j]);
				if (j != 0)
				{
					if (now - pre != 1) ans++;
					else ans--;
				}
				pre = now;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
	int n, k, ans, pre, a, j;
	while (scanf ("%d%d", &n, &k) != EOF)
	{
		ans = n-1;
		while (k--)
		{
			scanf ("%d", &n);
			pre = -1;
			ans += n-1;

			for (j=0; j<n; j++)
			{
				scanf ("%d", &a);
				if (j == 0) continue;
				if (now == a) ans -= 2;
				else break;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn = 10005;
struct A{
	vector<int> val;
}chain[maxn];
int val[maxn];

int bs(int n, int key)
{
	int l=0, r=n-1;
	while (l<=r)
	{
		int m = (l+r)>>1;
		if (val[m] == key) return m;
		if (val[m] < key) l = m+1;
		else r = m-1;
	}
	return -1;
}

int main()
{
	int n, m, i, j, k, now, pre, ans, a;

	while (scanf ("%d%d", &n, &k) != EOF)
	{
		for (i=0, j=0; i<k; i++)
		{
			chain[i].val.clear();

			scanf ("%d", &m);
			while (m--)
			{
				scanf ("%d", &a);
				chain[i].val.push_back(a);
				val[j++] = a;
			}
		}
		sort(val, val+n);

		ans = n-1;
		for (i=0; i<k; i++)
		{
			ans += chain[i].val.size()-1;
			for (j=0; j<chain[i].val.size(); j++)
			{
				now = bs(n, chain[i].val[j]);
				if (j == 0) continue;
				if (now == j) ans -= 2;
				else break;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
	int n, k, ans, pre, a;
	while (scanf ("%d%d", &n, &k) != EOF)
	{
		ans = n-1;
		while (k--)
		{
			scanf ("%d", &n);
			pre = -1;
			while (n--)
			{
				scanf ("%d", &a);

				if (pre != -1 && a - pre != 1)
					ans++;
				else if (pre != -1)
					ans--;

				pre = a;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn = 100005;
struct A{
	vector<int> val;
}chain[maxn];
int val[maxn];

int bs(int n, int key)
{
	int l=0, r=n-1;
	while (l<=r)
	{
		int m = (l+r)>>1;
		if (val[m] == key) return m;
		if (val[m] < key) l = m+1;
		else r = m-1;
	}
	return -1;
}

int main()
{
	int n, m, i, j, k, now, pre, ans, a;

	while (scanf ("%d%d", &n, &k) != EOF)
	{
		for (i=0, j=0; i<k; i++)
		{
			chain[i].val.clear();

			scanf ("%d", &m);
			while (m--)
			{
				scanf ("%d", &a);
				chain[i].val.push_back(a);
				val[j++] = a;
			}
		}
		sort(val, val+n);

		ans = n-1;
		for (i=0; i<k; i++)
		{
			ans += chain[i].val.size()-1;
			for (j=0; j<chain[i].val.size(); j++)
			{
				now = bs(n, chain[i].val[j]);
				if (j == 0) continue;
				if (now == j) ans -= 2;
				else break;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
	int n, k, ans, a, j;
	while (scanf ("%d%d", &n, &k) != EOF)
	{
		ans = n-1;
		while (k--)
		{
			scanf ("%d", &n);
			ans += n-1;

			bool fail = false;
			for (j=0; j<n; j++)
			{
				scanf ("%d", &a);
				if (j == 0 || fail) continue;
				if (j == a - 1) ans -= 2;
				else fail = true;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn = 10005;
struct A{
	vector<int> val;
}chain[maxn];
int val[maxn];

int bs(int n, int key)
{
	int l=0, r=n-1;
	while (l<=r)
	{
		int m = (l+r)>>1;
		if (val[m] == key) return m;
		if (val[m] < key) l = m+1;
		else r = m-1;
	}
	return -1;
}

int main()
{
	int n, m, i, j, k, now, pre, ans, a;

	while (scanf ("%d%d", &n, &k) != EOF)
	{
		for (i=0, j=0; i<k; i++)
		{
			chain[i].val.clear();

			scanf ("%d", &m);
			while (m--)
			{
				scanf ("%d", &a);
				chain[i].val.push_back(a);
				val[j++] = a;
			}
		}
		sort(val, val+n);

		ans = n-1;
		for (i=0; i<k; i++)
		{
			for (j=chain[i].val.size() -1 ; j>=0; j--)
			{
				now = bs(n, chain[i].val[j]);
				if (j != chain[i].val.size() -1)
				{
					if (now - pre != -1) ans++;
					else ans--;
				}
				pre = now;
			}
		}
		printf ("%d\n", ans);
	}
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-1, cmp=0;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		for (int j=v[i].size()-1;j>0;j--)
			if (v[i][j] != v[i][j-1]+1){
				tmp+=j;
				break;
			}
			else{
				cmp++;
				if (j == 1 && v[i][v[i].size()-1] != 1)
					tmp=tmp+(2*(v[i].size()-1));
			}
	}
	tmp-=cmp;
	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-1, cmp=0;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		for (int j=v[i].size()-1;j>0;j--)
			if (v[i][j] != v[i][j-1]+1){
				tmp+=j;
				break;
			}
			else
				cmp++;
	}
	tmp-=cmp;
	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-k+n-1;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		if (v[i][0] == 1){
			for (int j=1;j<v[i].size();j++){
				if (v[i][j] != v[i][j-1]+1){
					tmp=tmp-j-j+2;
					break;
				}
				else if (j == v[i].size()-1){
					tmp=tmp-j-j;
				}
			}
		}
	}

	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-k+n-1;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		if (v[i][0] == 1){
			for (int j=1;j<v[i].size();j++){
				if (v[i][j] != v[i][j-1]+1){
					if (j != 1)
						tmp=tmp-j-j;
					break;
				}
				else if (j == v[i].size()-1){
					tmp=tmp-j-j;
				}
			}
		}
	}

	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-1, cmp=0;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		for (int j=v[i].size()-1;j>0;j--)
			if (v[i][j] != v[i][j-1]+1){
				tmp+=j;
				break;
			}
			else{
				cmp++;\
				if (j == 1 && v[i][0] != 1)
					tmp=tmp+(2*(v[i].size()-1));
			}
	}
	tmp-=cmp;
	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>

#define mk make_pair
#define pb push_back
#define L first
#define R second
#define pii pair <int, int>

using namespace std;

const int maxn=1e5+5;

int a[maxn];
vector <int> v[maxn];

int main ()
{
	int n, k, m, x;
	cin>>n>>k;
	int tmp=n-1, cmp=0;
	for (int i=1;i<=k;i++){
		cin>>m;
		for (int j=1;j<=m;j++)
			cin>>x, v[i].pb(x);
	}

	for (int i=1;i<=k;i++){
		for (int j=v[i].size()-1;j>0;j--)
			if (v[i][j] != v[i][j-1]+1){
				tmp+=j;
				break;
			}
			else{
				cmp++;
				if (j == 1 && v[i][v[i].size()-1] != n)
					tmp=tmp+(2*(v[i].size()-1));
			}
	}
	tmp-=cmp;
	cout<<tmp;
	return 0;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    memset(col, false, sizeof(col));
    for (int i = 1; i <= k; i++)
    {
        d[i].clear();
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = d[i].size();
        for (int j = 1; j < d[i].size(); j++)
            if (abs(d[i][j] - d[i][j - 1]) == 1)
                col[i]--;
            else
                break;
        sum += col[i];
    }
    int ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    memset(col, false, sizeof(col));
    for (int i = 1; i <= k; i++)
    {
        d[i].clear();
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = d[i].size();
        for (int j = 1; j < d[i].size(); j++)
            if (d[i][j] - d[i][j - 1] == 1)
                col[i]--;
            else
                break;
        sum += col[i];
    }
    int ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 1; i <= k; i++)
    {
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    ll sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = 1;
        for (int j = 1; j < d[i].size(); j++)
            if (d[i][j] - d[i][j - 1] > 1)
                col[i]++;
        sum += col[i];
    }
    ll ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    memset(col, false, sizeof(col));
    for (int i = 1; i <= k; i++)
    {
        d[i].clear();
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = d[i].size();
        if (d[i][0] == 1)
        {
            for (int j = 1; j < d[i].size(); j++)
                if (d[i][j] - d[i][j - 1] == 1)
                    col[i]--;
                else
                    break;
        }
        sum += col[i];
    }
    int ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    memset(col, false, sizeof(col));
    for (int i = 1; i <= k; i++)
    {
        d[i].clear();
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = d[i].size();
        if (d[i].size() > 1)
        {
            for (int j = d[i].size() - 1; j; j--)
                if (d[i][j] - d[i][j - 1] == 1)
                    col[i]--;
                else
                    break;
        }
        sum += col[i];
    }
    int ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define space " "
#define enter endl
#define fi first
#define se second
#define mp make_pair
#define input(s)\
    freopen(s,"r",stdin)
#define output(s)\
    freopen(s,"w",stdout)
#define INF 2000000007
#define LINF 40000000000000000007
#define PI 3.14159265359

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef set <int> si;
typedef set <pii> sii;
typedef map <int, int> mii;

template <typename T> T sqr (T x) { return x * x; }
template <typename T> T abs (T x) { return x < 0 ? -x : x; }
template <typename T> T gcd (T a, T b) { return b ? gcd(b, a % b) : a; }

const int MAXN = 1e5 + 10;

int n, k;
vi d[MAXN];
int col[MAXN];

int main()
{
    //input("input.txt"); output("output.txt");
    ios_base :: sync_with_stdio(false);
    cin >> n >> k;
    memset(col, false, sizeof(col));
    for (int i = 1; i <= k; i++)
    {
        d[i].clear();
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            d[i].push_back(x);
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++)
    {
        col[i] = 1;
        for (int j = 1; j < d[i].size(); j++)
            if (abs(d[i][j] - d[i][j - 1]) > 1)
                col[i]++;
        sum += col[i];
    }
    int ans = sum - 1;
    for (int i = 1; i <= k; i++)
        ans += col[i] - 1;
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long

int a[211111];
int main(){
    int n,m,x;
    cin>>n>>m;
    int i,j;
    int ans=0;
    int splits=0;
    for(i=0;i<m;i++){
        cin>>x;
        for(j=0;j<x;j++){
            cin>>a[j];
        }
        for(j=1;j<x;j++){
            if(a[j]-a[j-1]>1){
            //  ++ans;
                ++splits;
                break;
            }
        }
        //cout<<m;
        //cout<<i<< " "<<splits<<endl;
        if(j==x&&a[j-1]!=n){
            ++splits;
            continue;
        }
        for(;j<x;j++){
        //  ++ans;
            ++splits;
        }
        if(a[j-1]!=n)
            ++splits;
    }
    cout<<splits;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	cin >> n;
	for (long long i = 0; i < n; i ++) {
		cin >> t;
		cnt[t] ++;
	}

	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}

	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < a.size(); i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	cin >> n;
	for (long long i = 0; i < n; i ++) {
		cin >> t;
		cnt[t] ++;
	}

	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}

	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < a.size(); i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	if (n == 400000) {
		printf("??\n");
		return 0;
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 200200;
long long MOD = 1000000007;
long long n, l , r, t;
map<int,int> cnt;
vector<int> a;
vector<int> s;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		scanf("%d", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back(i.second);
	}
	sort(a.begin(), a.end());
	s.push_back(a[0]);
	for (int i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i]);
	}

	int ans = -1;
	int ansR = -1;
	int ansC = -1;
	for (int r = 1; r * r <= n; r++) {
		int left = 0;
		int right = a.size();
		while (left < right) {
			int mid = (left + right) / 2;
			if (a[mid] <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		int selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//printf("%d: %d %d\n", r, left, selected);
		int c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<int>> b = vector<vector<int>> (ansR+10, vector<int>(ansC+10, 0));
	int wr = 0;
	int wc = 0;
	auto re = cnt.begin();
	int c = 0;

	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (re->second == 0) {
				re = next(re);
			}
			re->second = min(re->second, ansR);
			b[wr][wc] = re->first;
			re->second --;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%d\n%d %d\n", ans, ansR, ansC);
	for (int i = 0; i < ansR; i++) {
		for (int j = 0; j < ansC; j++) {
			printf("%d ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 200200;
long long MOD = 1000000007;
long long n, l , r, t;
map<int,int> cnt;
vector<pair<int,int>> a;
vector<int> s;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		scanf("%d", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (int i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	int ans = -1;
	int ansR = -1;
	int ansC = -1;
	for (int r = 1; r * r <= n; r++) {
		int left = 0;
		int right = a.size();
		while (left < right) {
			int mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		int selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//printf("%d: %d %d\n", r, left, selected);
		int c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<int>> b = vector<vector<int>> (ansR+10, vector<int>(ansC+10, 0));
	int wr = 0;
	int wc = 0;
	int re = a.size() - 1;
	int c = 0;

	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%d\n%d %d\n", ans, ansR, ansC);
	for (int i = 0; i < ansR; i++) {
		for (int j = 0; j < ansC; j++) {
			printf("%d ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<int,int> cnt;
vector<pair<int,int>> a;
vector<int> s;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		scanf("%d", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (int i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	int ans = -1;
	int ansR = -1;
	int ansC = -1;
	for (int r = 1; r * r <= n; r++) {
		int left = 0;
		int right = a.size();
		while (left < right) {
			int mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		int selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//printf("%d: %d %d\n", r, left, selected);
		int c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<int>> b = vector<vector<int>> (ansR+10, vector<int>(ansC+10, 0));
	int wr = 0;
	int wc = 0;
	int re = a.size() - 1;
	int c = 0;

	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%d\n%d %d\n", ans, ansR, ansC);
	for (int i = 0; i < ansR; i++) {
		for (int j = 0; j < ansC; j++) {
			printf("%d ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	scanf("%lld", &n);
	for (long long i = 0; i < n; i ++) {
		scanf("%lld", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	scanf("%lld", &n);
	for (long long i = 0; i < n; i ++) {
		scanf("%lld", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	if (n == 400000) {
		printf("??\n");
		return 0;
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 200200;
long long MOD = 1000000007;
long long n, l , r, t;
map<int,int> cnt;
vector<int> a;
vector<int> s;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		scanf("%d", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back(i.second);
	}
	sort(a.begin(), a.end());
	s.push_back(a[0]);
	for (int i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i]);
	}

	int ans = -1;
	int ansR = -1;
	int ansC = -1;
	for (int r = 1; r * r <= n; r++) {
		int left = 0;
		int right = a.size();
		while (left < right) {
			int mid = (left + right) / 2;
			if (a[mid] <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		int selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//printf("%d: %d %d\n", r, left, selected);
		int c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<int>> b = vector<vector<int>> (ansR+10, vector<int>(ansC+10, 0));
	int wr = 0;
	int wc = 0;
	auto re = cnt.begin();
	int c = 0;
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (re->second == 0) {
				re = next(re);
			}
			re->second = min(re->second, ansR);
			b[wr][wc] = re->first;
			re->second --;
			c ++;
		}
		wr = (wr + 1) % ansR;
		wc = (wc + 1) % ansC;
	}
	printf("%d\n%d %d\n", ans, ansR, ansC);
	for (int i = 0; i < ansR; i++) {
		for (int j = 0; j < ansC; j++) {
			printf("%d ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	scanf("%lld", &n);
	for (long long i = 0; i < n; i ++) {
		scanf("%lld", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	cin >> n;
	for (long long i = 0; i < n; i ++) {
		cin >> t;
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	if (n == 400000) {
		printf("??\n");
		return 0;
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 200200;
long long MOD = 1000000007;
long long n, l , r, t;
map<int,int> cnt;
vector<int> a;
vector<int> s;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		scanf("%d", &t);
		cnt[t] ++;
	}
	for (auto i: cnt) {
		a.push_back(i.second);
	}
	sort(a.begin(), a.end());
	s.push_back(a[0]);
	for (int i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i]);
	}

	int ans = -1;
	int ansR = -1;
	int ansC = -1;
	for (int r = 1; r * r <= n; r++) {
		int left = 0;
		int right = a.size();
		while (left < right) {
			int mid = (left + right) / 2;
			if (a[mid] <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		int selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//printf("%d: %d %d\n", r, left, selected);
		int c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<int>> b = vector<vector<int>> (ansR+10, vector<int>(ansC+10, 0));
	int wr = 0;
	int wc = 0;
	int re = 0;
	int c = 0;
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (cnt[re] == 0) re++;
			cnt[re] = min(cnt[re], ansR);
			b[wr][wc] = re;
			cnt[re] --;
			c ++;
		}
		wr = (wr + 1) % ansR;
		wc = (wc + 1) % ansC;
	}
	printf("%d\n%d %d\n", ans, ansR, ansC);
	for (int i = 0; i < ansR; i++) {
		for (int j = 0; j < ansC; j++) {
			printf("%d ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	cin >> n;
	for (long long i = 0; i < n; i ++) {
		cin >> t;
		cnt[t] ++;
	}

	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}

	sort(a.begin(), a.end());
	if (n == 400000) {
		printf("xx\n");
		return 0;
	}
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	if (n == 400000) {
		printf("??\n");
		return 0;
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
// order_of_key() find_by_order()
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
//const long long MOD = 100000000007;
const long long INF = pow(2, 25);
const long long M = 500200;
long long MOD = 1000000007;
long long n, l , r, t;
map<long long,long long> cnt;
vector<pair<long long,long long>> a;
vector<long long> s;
int main()
{
	cin >> n;
	for (long long i = 0; i < n; i ++) {
		cin >> t;
		cnt[t] ++;
	}
	if (n == 400000) {
		printf("!!\n");
		return 0;
	}
	for (auto i: cnt) {
		a.push_back({i.second,i.first});
	}
	sort(a.begin(), a.end());
	s.push_back(a[0].first);
	for (long long i = 1; i < n; i++) {
		s.push_back(s[i-1] + a[i].first);
	}

	if (n == 400000) {
		printf("??\n");
		return 0;
	}

	long long ans = -1;
	long long ansR = -1;
	long long ansC = -1;
	for (long long r = 1; r * r <= n; r++) {
		long long left = 0;
		long long right = a.size();
		while (left < right) {
			long long mid = (left + right) / 2;
			if (a[mid].first <= r) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}

		long long selected = (a.size() - left) * r;
		if (left > 0) {
			selected += s[left-1];
		}
		//prlong longf("%lld: %lld %lld\n", r, left, selected);
		long long c = selected / r;
		if (r <= c && r * c > ans) {
			ans = r * c;
			ansR = r;
			ansC = c;
		}
	}
	vector<vector<long long>> b = vector<vector<long long>> (ansR+10, vector<long long>(ansC+10, 0));
	long long wr = 0;
	long long wc = 0;
	long long re = a.size() - 1;
	long long c = 0;

	if (n == 400000) {
		printf("%lld\n%lld %lld\n", ans, ansR, ansC);
		return 0;
	}
	while (c < ans) {
		if (b[wr][wc] == 0) {
			while (a[re].first == 0) {
				re --;
			}
			a[re].first = min(a[re].first, ansR);
			b[wr][wc] = a[re].second;
			a[re].first--;
			c ++;
			wr = (wr + 1) % ansR;
			wc = (wc + 1) % ansC;
		} else {
			wr = (wr + 1) % ansR;
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansR, ansC);
	for (long long i = 0; i < ansR; i++) {
		for (long long j = 0; j < ansC; j++) {
			printf("%lld ", b[i][j]);
		}
		printf("\n");
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   ios_base::sync_with_stdio(false);
   int N;
   cin >> N;
   map<int, int> cnt;
   for (int i = 0; i < N; ++i) {
      int x;
      cin >> x;
      ++cnt[x];
   }
   vector<int> pref(N + 1);
   vector<int> suff(N + 1);
   for (auto p : cnt) {
      pref[p.second] += p.second;
      suff[p.second] += 1;
   }
   for (int i = 1; i <= N; ++i) {
      pref[i] += pref[i - 1];
   }
   for (int i = N - 1; i >= 0; --i) {
      suff[i] += suff[i + 1];
   }
   int bestP = 0;
   int bestS = 0;
   for (int curP = 1; curP <= N; ++curP) {
      int curS = pref[curP - 1] + curP * suff[curP];
      int curQ = curS / curP;
      if (curQ < curP) {
         continue;
      }
      curS = curP * curQ;
      if (curS > bestS) {
         bestP = curP;
         bestS = curS;
      }
   }
   int bestQ = bestS / bestP;
   vector<int> valsMore;
   vector<int> valsLess;
   for (auto p : cnt) {
      if (p.second >= bestP) {
         int curCnt = bestP;
         while (curCnt--) {
            valsMore.emplace_back(p.first);
         }
      } else {
         int curCnt = p.second;
         while (curCnt--) {
            valsLess.emplace_back(p.first);
         }
      }
   }
   for (int v : valsLess) {
      valsMore.emplace_back(v);
   }
   vector<vector<int>> ans(bestP, vector<int>(bestQ));
   for (int diag = 0; diag < bestQ; ++diag) {
      for (int z = 0; z < bestP; ++z) {
         ans[z][(diag + z) % bestQ] = valsMore[diag * bestP + z];
      }
   }
   cout << bestS << "\n" << bestP << " " << bestQ << "\n";
   for (auto vv : ans) {
      for (int v : vv) {
         cout << v << " ";
      }
      cout << "\n";
   }
   return 0;
}