#include <bits/stdc++.h>

using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    int x = 0;
    cin >> x;

    vector<int> line (x,0);
    int dangerous = 0;
    for(int i = 0; i < x; i++){
        int birds;
        cin >> birds;

        vector<int> next (birds, 0);
        for(int j = 0; j < birds; j++){
            int index;
            cin >> index;

            for(int k = index+1; k < x; k++){
                dangerous += line[k];
            }
            next[j] = index;
        }

        for(auto num : next){
            ++line[num];
        }
    }

    cout << dangerous << endl;
}