1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cstdio>
#include <vector>

const int max_n = 128;
const int max_d = 100;
const int mod = 1000000;

int n;

std::vector<int> G[max_n];

long long nwd(long long a, long long b) {
  return b == 0 ? a : nwd(b, a % b);
}

struct BigInt {
 int D[max_d];
};

void set(BigInt& a, int value) {
 for (int i = 0; i < max_d; ++i) a.D[i] = 0;
 a.D[0] = value;
}

void Add(BigInt& a, const BigInt& b) {
  int ret = 0;
  for (int i = 0; i < max_d; ++i) {
    a.D[i] += b.D[i] + ret;
    ret = 0;
    if (a.D[i] >= mod) {
      ret = 1;
      a.D[i] -= mod;
    }
  }
}

void Div(BigInt& ret, const BigInt& a, int d) {
  long long rem = 0;
  for (int i = max_d - 1; i >= 0; --i) {
    rem = rem * mod + a.D[i];
    if (rem >= d) {
      ret.D[i] = rem / d;
      rem = rem % d;
    } else {
      ret.D[i] = 0;
    }
  }
}

int Mod(const BigInt& a, int m) {
  int ret = 0;
  for (int i = max_d - 1; i >= 0; --i) {
    ret = ((long long)(ret) * mod + a.D[i]) % m;
  }
  return ret;
}

void Mult(BigInt& a, int m) {
  int ret = 0;
  for (int i = 0; i < max_d; ++i) {
    long long r = (long long)(a.D[i]) * m + ret;
    a.D[i] = r % mod;
    ret = r / mod;
  }
}

void Print(const BigInt& a) {
 for (int i = max_d - 1; i >= 0; --i) {
   if (a.D[i] > 0) {
     printf("%d", a.D[i]);
     for (int j = i - 1; j >= 0; --j) {
       printf("%06d", a.D[j]);
     }
     printf("\n");
     return;
   }
 }
 printf("1\n");
}

BigInt signals[max_n];
BigInt period;
BigInt tmp;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int cnt;
    scanf("%d", &cnt);
    for (int j = 0; j < cnt; ++j) {
      int u;
      scanf("%d", &u);
      --u;
      G[i].push_back(u);
    }
  }

  set(period, G[0].size());
  while (true) {
    for (int i = 0; i < n; ++i) {
      set(signals[i], 0);
    }
    signals[0] = period;
 // printf("Period\n");
 // Print(period);
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if (G[i].size() == 0) continue;
  //    Print(signals[i]);
      if (Mod(signals[i], G[i].size()) == 0) {
 //        printf("Divide %d\n", G[i].size());
//         Print(signals[i]);
         Div(tmp, signals[i], G[i].size());
//         Print(tmp);
//         printf("Divide DONE\n");
         for (int j = 0; j < G[i].size(); ++j) {
 //        printf("Sum\n");
//Print(signals[G[i][j]]);
//Print(tmp);
           Add(signals[G[i][j]], tmp);
//Print(signals[G[i][j]]);
//         printf("Sum done\n");
         }
      } else {
//        printf("Multiply %d %d\n", G[i].size(), G[i].size() / nwd(Mod(signals[i], G[i].size()), G[i].size()));
//        Print(period);
        Mult(period, G[i].size() / nwd(Mod(signals[i], G[i].size()), G[i].size()));
//        Print(period);
//         printf("Multiply DONE\n");
        ok = false;
//        printf("END => %d\n", i);
        break;
      }
    }
//break;
    if (ok) break;
  }
  Print(period);
  return 0;
}