#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef struct _dot{
    int x;
    int y;
}Dot;
bool Check(vector<Dot> list,int Assumption[]){
    
    for(int i=0;i<list.size();i++){
        if(Assumption[list[i].x ] != list[i].y) return false;
    }
    return true;
}
int main(){
    
    int N; cin >> N;
    vector<vector<Dot>> list(N);
    
    for(int i=0;i<N;i++){
        int k; cin >> k;
        for(int j=0;j<k;j++){
            int x,y; cin >> x >> y;
            Dot k; k.x = x-1; k.y = y;
            list.at(i).push_back(k);
        }
    }
    
    
    int ans = 0;
    int size = pow(2,N) - 1;
    for (int bits = size;bits >=0; bits--) {
        
        int Assumption[N];
        int num = 0;
        for(int i=0;i<N;i++){
            int mask = 1 << i;
            if(bits & mask){Assumption[i] = 1;num++;}
            else{Assumption[i] = 0;}
        }
        for (int i=0;i<N;i++) {
            int mask = 1 << i;
            if (bits & mask) {
            
                if(Check(list.at(i),Assumption) == false) break;
            }
            if(i== N-1 && ans < num) ans = num;
        }
 
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
typedef pair<int,int> P;
const int MOD = 1000000007;
const ll INF = 1e10;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template<class T> inline bool chmin(T& a, T b){if(a>b){a = b;return 1;}return 0;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a = b;return 1;}return 0;}
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 




////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////

int n,m,a[310][310], ans;
int cnt[310];bool used[310];
void j(){
  P mx;
  while(true){
    rep(i,m)cnt[i] = 0;
    rep(i,n){
      rep(j,m){
        if(!used[a[i][j]]){
          cnt[a[i][j]]++;
          break;
        }
      }
    }
    mx = P(-1, -1);
    rep(i,m)if(mx.first < cnt[i])mx = P(cnt[i], i);
    chmin(ans, mx.first);
    used[mx.second] = true;
    bool r = false;rep(i,m)if(!used[i])r = true;
    if(!r)return;
  }
}

signed main(){
  cin>>n>>m;
  rep(i,n)rep(j,m){
    cin>>a[i][j];a[i][j]--;
  }
  ans = INF;j();
  cout << ans << endl;
}
