#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
int x[20][20], y[20][20],a[20];
int main() {
	int n;
	cin >> n;
	rep(i,n) {
		cin >> a[i];
		rep(j,a[i]) {
			cin >> x[i][j] >> y[i][j];
			x[i][j]--;
		}
	}

	int ans = 0;
	rep(bit, (1<<n)) {
		bool ok = true;
		rep(i,n) {
			if (!((bit >> i) & 1)) continue;
			rep(j,a[i]) {
				if (((bit>>x[i][j]) & 1) ^ y[i][j]) ok = false;
			}
		}
		if (ok) ans = max(ans, __builtin_popcount(bit));
	}
	cout << ans << endl;
	return 0;
}
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <numeric>
#include <functional>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
typedef long long ll;

int N, A[110], cnt;

int main(int argc, char *argv[])
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>> N;
  REP(i,N) cin >> A[i];
  bool flag = true;
  while (flag) {
    flag = false;
    RFOR(j,1,N){
      if (A[j] < A[j-1]) {
        swap(A[j], A[j-1]);
        flag = true;
        ++cnt;
      }
    }
  }

  REP(i,N){
    cout << A[i];
    if (i != N-1) {
      cout << " ";
    } else {
      cout << "\n";
    }
  }
  cout << cnt << endl;

  return 0;
}