#include using namespace std; #define ll long long int main() { ll pct; cin >> pct; ll tot = 0; vector > linia1; vector > linia2; linia1.resize(pct); linia2.resize(pct); for (ll i = 0; i < pct; i++) { ll numb; cin >> numb; for (ll g = 0; g < numb; g++) { ll to; cin >> to; linia1[i].push_back(to); linia2[to].push_back(i); } } ll bot = 0; ll top = 0; for (ll i = 0; i < pct; i++) { ll debili = 0; ll debilitop = 0; ll debilibot = 0; for (ll g = 0; g < linia1[i].size(); g++) { if (linia1[i][g] == i) { debili += 1; } else if (linia1[i][g] > i) { debilitop +=1; } else { bot-=1; } } for (ll g = 0; g < linia2[i].size(); g++) { if (linia2[i][g] == i) { } else if (linia2[i][g] > i) { debilibot +=1; } else { top-=1; } } tot += debili*(top+bot); tot += debilibot*top; tot += debilitop*bot; tot += debilitop*debilibot; bot+= debilibot; top+= debilitop; } cout << tot; cout << endl; }