import java.util.*;
import java.io.*;

public class Solution{
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(System.out);
		String[] temp=br.readLine().trim().split(" ");

		int numSequences=Integer.parseInt(temp[0]);
		int[][] arr=new int[numSequences][];
		for(int i=0;i<numSequences;i++){
		    temp=br.readLine().trim().split(" ");
		    int n=Integer.parseInt(temp[0]);
		    arr[i]=new int[n];
		    for(int j=0;j<n;j++){
		        arr[i][j]=Integer.parseInt(temp[j+1]);
		    }
		}
		out.println(numPairs(arr));
		out.flush();
		out.close();
	}
	public static long numPairs(int[][] arr)
	{
	    long numPairs=0;
	    int numSequences=arr.length;
	    FenwickTree fenTree=new FenwickTree(1000000+1);
	    int ascentContainingSequences=0;
	    for(int i=numSequences-1;i>=0;i--){
	        int n=arr[i].length;
	        int max=Integer.MIN_VALUE;
	        boolean isAscentPresent=false;
	        for(int j=0;j<n;j++){
	            if(j-1>=0 && arr[i][j]>arr[i][j-1])
	            {
	                isAscentPresent=true;
	            }
	            max=Math.max(max,arr[i][j]);
	        }
	        if(isAscentPresent)
	        {
	            ascentContainingSequences++;
	        }
	        else{
	            fenTree.update(max,1);
	        }
	    }
	    for(int i=0;i<numSequences;i++){
	        int n=arr[i].length;
	        int min=Integer.MAX_VALUE;
	        int max=Integer.MIN_VALUE;
	        boolean isAscentPresent=false;
	        for(int j=0;j<n;j++){
	            if(j-1>=0 && arr[i][j]>arr[i][j-1])
	            {
	                isAscentPresent=true;
	            }
	            min=Math.min(min,arr[i][j]);
	        }
	       if(isAscentPresent)
	       {
	           numPairs+=numSequences;
	       }
	       else{
	           numPairs+=(ascentContainingSequences);
	           int numSmallerOrEqual=fenTree.getPrefixSum(min);
	           int numGreater=numSequences-ascentContainingSequences-numSmallerOrEqual;
	           numPairs+=numGreater;
	       }
	    }
	    return numPairs;
	}
}

class FenwickTree
{
    private int[] BIT;
    private int n;

    public FenwickTree(int n){
        this.n=n;
        BIT=new int[n+1];
    }
    public void update(int index,int increment)
    {
        while(index<=n){
            BIT[index]+=increment;
            index+=(index&-index);
        }
    }
    public int getPrefixSum(int index){
        int ans=0;
        while(index>0){
            ans+=BIT[index];
            index-=(index&-index);
        }
        return ans;
    }
}


import java.util.*;
import java.io.*;

public class Solution{
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(System.out);
		String[] temp=br.readLine().trim().split(" ");

		int numSequences=Integer.parseInt(temp[0]);
		int[][] arr=new int[numSequences][];
		for(int i=0;i<numSequences;i++){
		    temp=br.readLine().trim().split(" ");
		    int n=Integer.parseInt(temp[0]);
		    arr[i]=new int[n];
		    for(int j=0;j<n;j++){
		        arr[i][j]=Integer.parseInt(temp[j+1]);
		    }
		}
		out.println(numPairs(arr));
		out.flush();
		out.close();
	}
	public static int numPairs(int[][] arr)
	{
	    int numPairs=0;
	    int numSequences=arr.length;
	    FenwickTree fenTree=new FenwickTree(1000000+1);
	    int ascentContainingSequences=0;
	    for(int i=numSequences-1;i>=0;i--){
	        int n=arr[i].length;
	        int max=Integer.MIN_VALUE;
	        boolean isAscentPresent=false;
	        for(int j=0;j<n;j++){
	            if(j-1>=0 && arr[i][j]>arr[i][j-1])
	            {
	                isAscentPresent=true;
	            }
	            max=Math.max(max,arr[i][j]);
	        }
	        if(isAscentPresent)
	        {
	            ascentContainingSequences++;
	        }
	        else{
	            fenTree.update(max,1);
	        }
	    }
	    for(int i=0;i<numSequences;i++){
	        int n=arr[i].length;
	        int min=Integer.MAX_VALUE;
	        int max=Integer.MIN_VALUE;
	        boolean isAscentPresent=false;
	        for(int j=0;j<n;j++){
	            if(j-1>=0 && arr[i][j]>arr[i][j-1])
	            {
	                isAscentPresent=true;
	            }
	            min=Math.min(min,arr[i][j]);
	        }
	       if(isAscentPresent)
	       {
	           numPairs+=numSequences;
	       }
	       else{
	           numPairs+=(ascentContainingSequences);
	           int numSmallerOrEqual=fenTree.getPrefixSum(min);
	           int numGreater=numSequences-ascentContainingSequences-numSmallerOrEqual;
	           numPairs+=numGreater;
	       }
	    }
	    return numPairs;
	}
}

class FenwickTree
{
    private int[] BIT;
    private int n;

    public FenwickTree(int n){
        this.n=n;
        BIT=new int[n+1];
    }
    public void update(int index,int increment)
    {
        while(index<=n){
            BIT[index]+=increment;
            index+=(index&-index);
        }
    }
    public int getPrefixSum(int index){
        int ans=0;
        while(index>0){
            ans+=BIT[index];
            index-=(index&-index);
        }
        return ans;
    }
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws IOException
	{
		// your code goes here
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		int a[][]=new int[n][2];
		int arr[]=new int[1000001];
		int c=0;
		for(int i=0;i<n;i++)
		{
		    String s[]=br.readLine().trim().split(" ");
		    int len=Integer.parseInt(s[0]);
		    int max=Integer.parseInt(s[1]);
		    int min=max,curr=max,f=0;
		    for(int j=2;j<1+len;j++)
		    {
		        int xx=Integer.parseInt(s[j]);
		        if(xx>curr)
		        {
		            f=1;break;
		        }
		        max=Math.max(max,xx);
		        min=Math.min(min,xx);
		        curr=xx;
		    }
		    if(f==1)
		    {
		        c++;
		    }
		    else
		    {
		        arr[max]++;
		        a[i][0]=min;
		        a[i][1]=max;
		    }
		    //System.out.println(max+" "+min+" "+curr);
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    arr[i]+=arr[i+1];
		}
		long res=1L*c*n;
		res+=(1L*c*(n-c));
		for(int i=0;i<n;i++)
		{
		    if(a[i][0]>0)
		    {
		        int max=a[i][1];
		        if(max!=a[i][0])
		        {
		            //arr[max]--;
		            res+=arr[a[i][0]+1];
		            //arr[max]++;
		        }
		        else
		        res+=arr[a[i][0]+1];
		    }
		}
		System.out.println(res);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws IOException
	{
		// your code goes here
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		int a[][]=new int[n][2];
		int arr[]=new int[1000001];
		int c=0;
		for(int i=0;i<n;i++)
		{
		    String s[]=br.readLine().trim().split(" ");
		    int len=Integer.parseInt(s[0]);
		    int max=Integer.parseInt(s[1]);
		    int min=max,curr=max,f=0;
		    a[i][0]=a[i][1]=-1;
		    for(int j=2;j<1+len;j++)
		    {
		        int xx=Integer.parseInt(s[j]);
		        if(xx>curr)
		        {
		            f=1;break;
		        }
		        max=Math.max(max,xx);
		        min=Math.min(min,xx);
		        curr=xx;
		    }
		    if(f==1)
		    {
		        c++;
		    }
		    else
		    {
		        arr[max]++;
		        a[i][0]=min;
		        a[i][1]=max;
		    }
		    //System.out.println(max+" "+min+" "+curr);
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    arr[i]+=arr[i+1];
		}
		long res=1L*c*n;
		res+=(1L*c*(n-c));
		for(int i=0;i<n;i++)
		{
		    if(a[i][0]>=0)
		    {

		        res+=arr[a[i][0]+1];
		    }
		}
		System.out.println(res);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws IOException
	{
		// your code goes here
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		int a[][]=new int[n][2];
		int arr[]=new int[1000002];
		int c=0;
		for(int i=0;i<n;i++)
		{
		    String s[]=br.readLine().trim().split(" ");
		    int len=Integer.parseInt(s[0]);
		    int max=Integer.parseInt(s[1]);
		    int min=max,curr=max,f=0;
		    a[i][0]=a[i][1]=-1;
		    for(int j=2;j<1+len;j++)
		    {
		        int xx=Integer.parseInt(s[j]);
		        if(xx>curr)
		        {
		            f=1;break;
		        }
		        max=Math.max(max,xx);
		        min=Math.min(min,xx);
		        curr=xx;
		    }
		    if(f==1)
		    {
		        c++;
		    }
		    else
		    {
		        arr[max]++;
		        a[i][0]=min;
		        a[i][1]=max;
		    }
		    //System.out.println(max+" "+min+" "+curr);
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    arr[i]+=arr[i+1];
		}
		long res=1L*c*n;
		res+=(1L*c*(n-c));
		for(int i=0;i<n;i++)
		{
		    if(a[i][0]>=0)
		    {

		        res+=arr[a[i][0]+1];
		    }
		}
		System.out.println(res);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int a[][]=new int[2][1000001];
		long pairs=0;
		for(int i=0;i<n;i++)
		{
		    int len=sc.nextInt();
		    int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
		    int prev=max,f=0;
		    for(int j=0;j<len;j++)
		    {
		        int num=sc.nextInt();
		        min=Math.min(min,num);
		        max=Math.max(max,num);
		        if(num>prev)
		        {
		            f=1;
		        }
		        prev=num;
		    }
		    a[0][max]++;
		    a[1][min]++;
		    if(f==1)
		    {
		        pairs+=n;
		        a[0][max]--;
		    }
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    a[0][i]+=a[0][i+1];
		}
		//long pairs=0;
		for(int i=0;i<1000000;i++)
		{
		    pairs+=(1L*a[1][i]*a[0][i+1]);
		}
		System.out.println(pairs);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws IOException
	{
		// your code goes here
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		int a[][]=new int[n][2];
		int arr[]=new int[1000001];
		int c=0;
		for(int i=0;i<n;i++)
		{
		    String s[]=br.readLine().trim().split(" ");
		    int len=Integer.parseInt(s[0]);
		    int max=Integer.parseInt(s[1]);
		    int min=max,curr=max,f=0;
		    for(int j=2;j<1+len;j++)
		    {
		        int xx=Integer.parseInt(s[j]);
		        if(xx>curr)
		        {
		            f=1;break;
		        }
		        max=Math.max(max,xx);
		        min=Math.min(min,xx);
		        curr=xx;
		    }
		    if(f==1)
		    {
		        c++;
		    }
		    else
		    {
		        arr[max]++;
		        a[i][0]=min;
		        a[i][1]=max;
		    }
		    //System.out.println(max+" "+min+" "+curr);
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    arr[i]+=arr[i+1];
		}
		long res=1L*c*n;
		res+=(1L*c*(n-c));
		for(int i=0;i<n;i++)
		{
		    if(a[i][0]>0)
		    {
		        int max=a[i][1];
		        if(max!=a[i][0])
		        {
		            arr[max]--;
		            res+=arr[a[i][0]+1];
		            arr[max]++;
		        }
		        else
		        res+=arr[a[i][0]+1];
		    }
		}
		System.out.println(res);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int a[][]=new int[2][1000001];
		long pairs=0;
		for(int i=0;i<n;i++)
		{
		    int len=sc.nextInt();
		    int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
		    int prev=min,f=0;
		    for(int j=0;j<len;j++)
		    {
		        int num=sc.nextInt();
		        min=Math.min(min,num);
		        max=Math.max(max,num);
		        if(num>prev)
		        {
		            f=1;
		        }
		        prev=num;
		    }
		    a[0][max]++;
		    a[1][min]++;
		    if(f==1)
		    {
		        pairs+=n;
		        a[0][max]--;
		    }
		}
		for(int i=1000000-1;i>=0;i--)
		{
		    a[0][i]+=a[0][i+1];
		}
		//long pairs=0;
		for(int i=0;i<1000000;i++)
		{
		    pairs+=(1L*a[1][i]*a[0][i+1]);
		}
		System.out.println(pairs);
	}
}


import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;

public class Main {

    public static void main(String[] args) throws Exception {
        long startTime = System.nanoTime();
        int n = in.nextInt();
        int LIM = 1_000_000 + 40;
        int MAX = 1_000_000 + 50;
        int[] minima = new int[MAX];
        int[] maxima = new int[MAX];
        int[] count = new int[MAX];
        long increasing = 0;
        Arrays.fill(minima, -1);
        for (int i = 0; i < n; i++) {
            int l = in.nextInt();
            int min = -1;
            int max = -1;
            int prev = -1;
            boolean isIncreasing = false;
            while (l-- > 0) {
                int num = in.nextInt();
                if (prev != -1 && num > prev) {
                    isIncreasing = true;
                }
                if (min != -1) {
                    min = Math.min(min, num);
                } else {
                    min = num;
                }
                max = Math.max(num, max);
                prev = num;
            }
            if (isIncreasing) {
                increasing++;
            } else {
                minima[i] = min;
                maxima[i] = max;
            }
        }
        for (int i = n; i >= 0; i--) {
            count[maxima[i]]++;
        }
        for (int i = LIM; i >= 0; i--) {
            count[i] += count[i + 1];
        }
        for (int i = n; i >= 0; i--) {
            count[maxima[i]]--;
        }
        long ans = increasing * n + increasing * (n - increasing);
        for (int i = 0; i < n; i++) {
            if (minima[i] != -1) {
                //err.println(minima[i] + " " + maxima[i] + " " + count[minima[i]]);
                ans += count[minima[i]];
            }
        }
        out.println(ans);
        long endTime = System.nanoTime();
        err.println("Execution Time : +" + (endTime - startTime) / 1000000 + " ms");
        exit(0);
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static void exit(int a) {
        out.close();
        err.close();
        System.exit(a);
    }

    static InputStream inputStream = System.in;
    static OutputStream outputStream = System.out;
    static OutputStream errStream = System.err;
    static InputReader in = new InputReader(inputStream);
    static PrintWriter out = new PrintWriter(outputStream);
    static PrintWriter err = new PrintWriter(errStream);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

}


import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;

public class Main {

    public static void main(String[] args) throws Exception {
        long startTime = System.nanoTime();
        int n = in.nextInt();
        int LIM = 1_000_00;
        int MAX = 1_000_000 + 50;
        int[] minima = new int[MAX];
        int[] maxima = new int[MAX];
        int[] count = new int[MAX];
        long increasing = 0;
        Arrays.fill(minima, -1);
        for (int i = 0; i < n; i++) {
            int l = in.nextInt();
            int min = -1;
            int max = -1;
            int prev = -1;
            boolean isIncreasing = false;
            while (l-- > 0) {
                int num = in.nextInt();
                if (prev != -1 && num > prev) {
                    isIncreasing = true;
                }
                if (min != -1) {
                    min = Math.min(min, num);
                } else {
                    min = num;
                }
                max = Math.max(num, max);
                prev = num;
            }
            if (isIncreasing) {
                increasing++;
            } else {
                minima[i] = min;
                maxima[i] = max;
            }
        }
        for (int i = n; i >= 0; i--) {
            count[maxima[i]]++;
        }
        for (int i = LIM; i >= 0; i--) {
            count[i] += count[i + 1];
        }
        for (int i = n; i >= 0; i--) {
            count[maxima[i]]--;
        }
        long ans = increasing * n + increasing * (n - increasing);
        for (int i = 0; i < n; i++) {
            if (minima[i] != -1) {
                //err.println(minima[i] + " " + maxima[i] + " " + count[minima[i]]);
                ans += count[minima[i]];
            }
        }
        out.println(ans);
        long endTime = System.nanoTime();
        err.println("Execution Time : +" + (endTime - startTime) / 1000000 + " ms");
        exit(0);
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static void exit(int a) {
        out.close();
        err.close();
        System.exit(a);
    }

    static InputStream inputStream = System.in;
    static OutputStream outputStream = System.out;
    static OutputStream errStream = System.err;
    static InputReader in = new InputReader(inputStream);
    static PrintWriter out = new PrintWriter(outputStream);
    static PrintWriter err = new PrintWriter(errStream);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class problem2 {
    public static void main(String[] args) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        long result = 0;
        ArrayList<Integer> minn = new ArrayList<>();
        ArrayList<Integer> maxx = new ArrayList<>();
        int s = n;
        for (int i = 0; i < n; i++) {
            StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
            int l = Integer.parseInt(tokenizer.nextToken());
            int min = -1;
            int max = -1;
            boolean flag = false;
            for (int j = 0; j < l; j++) {
                int a = Integer.parseInt(tokenizer.nextToken());
                if(j == 0){
                    min = a;
                    max = a;
                }
                else{
                    if(a > min) flag = true;
                    min = Math.min(min,a);
                    max = Math.max(max,a);
                }
            }
            if(flag) {
                result+=2*s-1;
                s--;
            }
            else {
                minn.add(min);
                maxx.add(max);
            }
        }
        maxx.sort(Comparator.naturalOrder());
        int size = maxx.size();
        if(size > 0) {
            TreeMap<Integer, Integer> map = new TreeMap<>();
            int prev = maxx.get(0);
            map.put(prev, size);
            for (int i = 1; i < size; i++) {
                int a = maxx.get(i);
                if (a != prev) {
                    prev = a;
                    map.put(a, size - i);
                }
            }
            for (int i = 0; i < size; i++) {
                int min = minn.get(i);
                Integer integer = map.higherKey(min);
                if (integer != null) {
                    result += map.get(integer);
                }
            }
        }
        System.out.println(result);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class problem2 {
    public static void main(String[] args) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        long result = 0;
        ArrayList<Integer> minn = new ArrayList<>();
        ArrayList<Integer> maxx = new ArrayList<>();
        int s = n;
        for (int i = 0; i < n; i++) {
            StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
            int l = Integer.parseInt(tokenizer.nextToken());
            int min = -1;
            int max = -1;
            boolean flag = false;
            for (int j = 0; j < l; j++) {
                int a = Integer.parseInt(tokenizer.nextToken());
                if(j == 0){
                    min = a;
                    max = a;
                }
                else{
                    if(a > min) flag = true;
                    min = Math.min(min,a);
                    max = Math.max(max,a);
                }
            }
            if(flag) {
                result+=2*s-1;
                s--;
            }
            else {
                minn.add(min);
                maxx.add(max);
            }
        }
        maxx.sort(Comparator.naturalOrder());
        TreeMap<Integer,Integer> map = new TreeMap<>();
        int size = maxx.size();
        int prev = maxx.get(0);
        map.put(prev,size);
        for (int i = 1; i < size; i++) {
            int a = maxx.get(i);
            if(a != prev){
                prev = a;
                map.put(a,size-i);
            }
        }
        for (int i = 0; i < size; i++) {
            int min = minn.get(i);
            Integer integer = map.higherKey(min);
            if(integer != null){
                result+=map.get(integer);
            }
        }
        System.out.println(result);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class problem2 {
    public static void main(String[] args) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        long result = 0;
        ArrayList<Integer> minn = new ArrayList<Integer>();
        ArrayList<Integer> maxx = new ArrayList<Integer>();
        int[] flags = new int[n];
        for (int i = 0; i < n; i++) {
            StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
            int l = Integer.parseInt(tokenizer.nextToken());
            int min = -1;
            int max = -1;
            boolean flag = false;
            for (int j = 0; j < l; j++) {
                int a = Integer.parseInt(tokenizer.nextToken());
                if(j == 0){
                    min = a;
                    max = a;
                }
                else{
                    if(a > min) flag = true;
                    min = Math.min(min,a);
                    max = Math.max(max,a);
                }
            }
            minn.add(min);
            maxx.add(max);
            if(flag) flags[i] = 1;
            else flags[i] = 0;
        }
       // Collections.sort(maxx);
        for (int i = 0; i < n; i++) {
            if(flags[i] == 1){
                result+=n;
            }else{
                int min = minn.get(i);
                for (int j = 0; j < n; j++) {
                    if(flags[j] == 1)result++;
                    else if(maxx.get(j) > min) result++;
                }
            }
        }
        System.out.println(result);

    }
}


// package Hello2020;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class ProblemB {
    public static int search(ArrayList<Integer> a,int n){
        int low=0;
        int high=a.size()-1;
        int ans=high;
        while(low<=high){
            int mid=low+(high-low)/2;
            int curr=a.get(mid);
            if(curr<=n){
                ans=mid;
                low=mid+1;
            }
            else{
                high=mid-1;
            }
        }
        return ans;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        int a[][]=new int[n][];
        boolean ismark[]=new boolean[n];
        ArrayList<Integer> max=new ArrayList<>();
        int numMark=0;
        for(int i=0;i<n;i++){
            String line[]=br.readLine().split(" ");
            int l=Integer.parseInt(line[0]);
            a[i]=new int[l];
            for(int j=0;j<l;j++){
                a[i][j]=Integer.parseInt(line[j+1]);
                if(j>0&&a[i][j]>a[i][j-1]){
                    ismark[i]=true;
                }
            }
            if(!ismark[i]){
                max.add(a[i][0]);
            }
            else{
                numMark++;
            }
        }
        Collections.sort(max);
//        System.out.println(numMark);
//        System.out.println(max.toString());
        long ans=0;
        for(int i=0;i<n;i++){
            if(ismark[i]){
                ans+=n;
            }
            else{
                ans+=numMark;
                int min=a[i][a[i].length-1];
                int num=search(max,min);
                ans+=(max.size()-1)-num;
//                System.out.println(num);
            }
//            System.out.println(ans);
        }
        System.out.println(ans);
    }
}


// package Hello2020;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class ProblemB {
    public static int search(ArrayList<Integer> a,int n){
        int low=0;
        int high=a.size()-1;
        int ans=a.size();
        while(low<=high){
            int mid=low+(high-low)/2;
            int curr=a.get(mid);
            if(curr<=n){
                low=mid+1;
            }
            else{
                ans=mid;
                high=mid-1;
            }
        }
        return ans;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        int a[][]=new int[n][];
        boolean ismark[]=new boolean[n];
        ArrayList<Integer> max=new ArrayList<>();
        int numMark=0;
        for(int i=0;i<n;i++){
            String line[]=br.readLine().split(" ");
            int l=Integer.parseInt(line[0]);
            a[i]=new int[l];
            for(int j=0;j<l;j++){
                a[i][j]=Integer.parseInt(line[j+1]);
                if(j>0&&a[i][j]>a[i][j-1]){
                    ismark[i]=true;
                }
            }
            if(!ismark[i]){
                max.add(a[i][0]);
            }
            else{
                numMark++;
            }
        }
        Collections.sort(max);
//        System.out.println(numMark);
//        System.out.println(max.toString());
        long ans=0;
        for(int i=0;i<n;i++){
            if(ismark[i]){
                ans+=n;
            }
            else{
                ans+=numMark;
                int min=a[i][a[i].length-1];
                int num=search(max,min);
                ans+=max.size()-num;
//                System.out.println(min+" "+num);
            }
//            System.out.println(ans);
        }
        System.out.println(ans);
    }
}


// package Hello2020;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class ProblemB {
    public static int search(ArrayList<Integer> a,int n){
        int low=0;
        int high=a.size()-1;
        int ans=0;
        while(low<=high){
            int mid=low+(high-low)/2;
            int curr=a.get(mid);
            if(curr<=n){
                ans=mid;
                low=mid+1;
            }
            else{
                high=mid-1;
            }
        }
        return ans;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        int a[][]=new int[n][];
        boolean ismark[]=new boolean[n];
        ArrayList<Integer> max=new ArrayList<>();
        int numMark=0;
        for(int i=0;i<n;i++){
            String line[]=br.readLine().split(" ");
            int l=Integer.parseInt(line[0]);
            a[i]=new int[l];
            for(int j=0;j<l;j++){
                a[i][j]=Integer.parseInt(line[j+1]);
                if(j>0&&a[i][j]>a[i][j-1]){
                    ismark[i]=true;
                }
            }
            if(!ismark[i]){
                max.add(a[i][0]);
            }
            else{
                numMark++;
            }
        }
        Collections.sort(max);
        long ans=0;
        for(int i=0;i<n;i++){
            if(ismark[i]){
                ans+=n;
            }
            else{
                ans+=numMark;
                int min=a[i][a[i].length-1];
                int num=search(max,min);
                ans+=(max.size()-1)-num;
//                System.out.println(num);
            }
//            System.out.println(ans);
        }
        System.out.println(ans);
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class h2020b {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int[] min=new int[n], max=new int[n];
		Arrays.fill(min,Integer.MAX_VALUE);
		Arrays.fill(max,Integer.MIN_VALUE);
		boolean[] asc=new boolean[n];
		long numasc=0L;

		for(int i=0;i<n;i++) {
			int x=scan.nextInt();
			int minsofar=Integer.MAX_VALUE;
			for(int j=0;j<x;j++) {
				int temp=scan.nextInt();
				if(temp>minsofar) asc[i]=true;
				min[i]=Math.min(min[i],temp);
				max[i]=Math.max(max[i],temp);
				minsofar=Math.min(minsofar,temp);
			}
			if(asc[i]) {
				min[i]=Integer.MAX_VALUE;
				max[i]=Integer.MIN_VALUE;
				numasc++;
			}
		}

		BIT b=new BIT(1000001);
		for(int i=0;i<n;i++) {
			if(!asc[i]) b.update(max[i],1);
		}

		long res=0L;
		for(int i=0;i<n;i++) {
			if(asc[i]) {
				res+=n;
			}
			else {
				int m=min[i];
				long greater=b.rangeSum(m+1,1000000);
				res+=greater;
				res+=numasc;
			}
		}

		System.out.println(res);
	}
	static class BIT {
		int n;
		int[] a;

		public BIT(int size) {
			this.n = size;
			a = new int[n];
		}
		void update(int i,int delta) {
			while(i<n) {
				a[i]+=delta;
				i+=Integer.lowestOneBit(i);
			}
		}
		int sum(int i) {
			int res=0;
			while(i>0) {
				res+=a[i];
				i-=Integer.lowestOneBit(i);
			}
			return res;
		}
		int rangeSum(int i,int j) {
			return sum(j)-sum(i-1);
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class h2020b {
	public static void main(String[] args) {
		FastScanner scan=new FastScanner();
		int n=scan.nextInt();
		int[] min=new int[n], max=new int[n];
		Arrays.fill(min,Integer.MAX_VALUE);
		Arrays.fill(max,Integer.MIN_VALUE);
		boolean[] asc=new boolean[n];
		long numasc=0L;

		for(int i=0;i<n;i++) {
			int x=scan.nextInt();
			int minsofar=Integer.MAX_VALUE;
			for(int j=0;j<x;j++) {
				int temp=scan.nextInt();
				if(temp>minsofar) asc[i]=true;
				min[i]=Math.min(min[i],temp);
				max[i]=Math.max(max[i],temp);
				minsofar=Math.min(minsofar,temp);
			}
			if(asc[i]) {
				min[i]=Integer.MAX_VALUE;
				max[i]=Integer.MIN_VALUE;
				numasc++;
			}
		}
		int[] sums=new int[1000001];
		for(int i=0;i<n;i++) {
			if(!asc[i]) sums[max[i]]++;
		}
		for(int i=1;i<sums.length;i++) sums[i]+=sums[i-1];

		long res=0L;
		for(int i=0;i<n;i++) {
			if(asc[i]) {
				res+=n;
			}
			else {
				int m=min[i];
				long greater=sums[1000000]-sums[m];
				res+=greater;
				res+=numasc;
			}
		}

		System.out.println(res);
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner() {
			try	{
				br = new BufferedReader(new InputStreamReader(System.in));
				st = new StringTokenizer(br.readLine());
			} catch (Exception e){e.printStackTrace();}
		}

		public String next() {
			if (st.hasMoreTokens())	return st.nextToken();
			try {st = new StringTokenizer(br.readLine());}
			catch (Exception e) {e.printStackTrace();}
			return st.nextToken();
		}

		public int nextInt() {return Integer.parseInt(next());}

		public long nextLong() {return Long.parseLong(next());}

		public double nextDouble() {return Double.parseDouble(next());}

		public String nextLine() {
			String line = "";
			if(st.hasMoreTokens()) line = st.nextToken();
			else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
			while(st.hasMoreTokens()) line += " "+st.nextToken();
			return line;
		}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
	static class Node {
		  public int min;
		  public int max;

		  Node(int n,int c) {
		    min = n;
		    max = c;

		  }
		}
	static int MD= 998244353 ;
	static int N=1000000;
    public static void main(String[] args) throws IOException {
    	OutputStream outputStream = System.out;
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

	static class TaskC {

	    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
	    	int t=1;
	    	while(t-->0) {
	    		int n=in.nextInt();
	    		boolean[] asc=new boolean[n];
	    		int[][] c=new int[n][2];
    			long res=0;
	    		long cnt=0;
	    		for(int i=0;i<n;i++) {
	    			int m=in.nextInt();
	    			int x=in.nextInt();
	    			int mx=x;
	    			int mn=x;
	    			for(int j=1;j<m;j++) {
	    				x=in.nextInt();
	    				if(x>mn) {
	    					asc[i]=true;
	    					cnt++;
	    				}
	    				mx=Math.max(x, mx);
	    				mn=Math.min(x, mn);

	    			}
	    			c[i][0]=mn;
	    			c[i][1]=mx;
	    		}
	    		res+=cnt*(n);
	    		res+=cnt*(n-cnt);
	    		cnt=n-cnt;

	    	//	System.out.println(res);
	    		Node[] a=new Node[(int) cnt];
	    		int j=0;
	    		for(int i=0;i<n && j<cnt ;i++) {
	    			if(asc[i]==false) {
	    				a[j]=new Node(c[i][0],c[i][1]);
	    				j++;
	    			}
	    		}

	    		nsort(a,0,(int)cnt);

	    		for(int i=0;i<cnt;i++) {
	    			int l=0,r=(int) cnt;
	    			while(l<r) {
	    				int m=l+(r-l)/2;
	    				if(a[m].max>a[i].min) {
	    					r=m;
	    				}
	    				else {
	    					l=m+1;
	    				}
	    		//		System.out.println(a[i].max+" l "+l);
	    			}

	    			res+=cnt-l;

	    		}
	    		out.println(res);

	        }
	    }

	}

    static long modexp(long x, long n)
    {
        if (n == 0) {
            return 1;
        }
        else if (n % 2 == 0) {
            return modexp((x * x) % MD, n / 2);
        }
        else {
            return (x * modexp((x * x) % MD, (n - 1) / 2) % MD);
        }
    }

     static long getInv(long b)
    {

        long  d = modexp(b, MD - 2);

        long ans =  (d % MD) ;

        return ans;
    }

    static class InputReader
    {
        BufferedReader br;
        StringTokenizer st;

        public InputReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }
    static void sort(int[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        sort(a, low, mid);
        sort(a, mid, high);
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    static void lsort(long[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        lsort(a, low, mid);
        lsort(a, mid, high);
        long[] temp = new long[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    public static long fexp(long x, long y, long p) {
        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;

            y = y >> 1;
            x = (x * x) % p;
        }
        return res;

    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);

    }
    static long lgcd(long a, long b)
    {
        if (b == 0)
            return a;
        return lgcd(b, a % b);

    }
    public static void nsort(Node[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        // recursively sort
        nsort(a, low, mid);
        nsort(a, mid, high);
        // merge two sorted subarrays
        Node[] temp = new Node[N];
        int i = low, j = mid;

        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j].max<a[i].max)
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
	static class Node {
		  public int min;
		  public int max;

		  Node(int n,int c) {
		    min = n;
		    max = c;

		  }
		}
	static int MD= 998244353 ;
	static int N=1000000;
    public static void main(String[] args) throws IOException {
    	OutputStream outputStream = System.out;
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

	static class TaskC {

	    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
	    	int t=1;
	    	while(t-->0) {
	    		int n=in.nextInt();
	    		boolean[] asc=new boolean[n];
	    		int[][] c=new int[n][2];
    			long res=0;
	    		long cnt=0;
	    		for(int i=0;i<n;i++) {
	    			int m=in.nextInt();
	    			int x=in.nextInt();
	    			int mx=x;
	    			int mn=x;
	    			for(int j=1;j<m;j++) {
	    				x=in.nextInt();
	    				if(x>mn) {
	    					asc[i]=true;
	    					cnt++;
	    				}
	    				mx=Math.max(x, mx);
	    				mn=Math.min(x, mn);

	    			}
	    			c[i][0]=mn;
	    			c[i][1]=mx;
	    		}
	    		res+=cnt*(n);
	    		res+=cnt*(n-cnt);
	    		cnt=n-cnt;

	    	//	System.out.println(res);
	    		Node[] a=new Node[(int) cnt];
	    		int j=0;
	    		for(int i=0;i<n;i++) {
	    			if(asc[i]==false) {
	    				a[j]=new Node(c[i][0],c[i][1]);
	    				j++;
	    			}
	    		}

	    		nsort(a,0,(int)cnt);

	    		for(int i=0;i<cnt;i++) {
	    			int l=0,r=(int) cnt;
	    			while(l<r) {
	    				int m=l+(r-l)/2;
	    				if(a[m].max>a[i].min) {
	    					r=m;
	    				}
	    				else {
	    					l=m+1;
	    				}
	    			}
	    		//	System.out.println(a[i].max+" l "+l);
	    			if(l<cnt) {
	    				res+=cnt-l;
	    			}

	    		}
	    		out.println(res);

	        }
	    }

	}

    static long modexp(long x, long n)
    {
        if (n == 0) {
            return 1;
        }
        else if (n % 2 == 0) {
            return modexp((x * x) % MD, n / 2);
        }
        else {
            return (x * modexp((x * x) % MD, (n - 1) / 2) % MD);
        }
    }

     static long getInv(long b)
    {

        long  d = modexp(b, MD - 2);

        long ans =  (d % MD) ;

        return ans;
    }

    static class InputReader
    {
        BufferedReader br;
        StringTokenizer st;

        public InputReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }
    static void sort(int[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        sort(a, low, mid);
        sort(a, mid, high);
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    static void lsort(long[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        lsort(a, low, mid);
        lsort(a, mid, high);
        long[] temp = new long[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    public static long fexp(long x, long y, long p) {
        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;

            y = y >> 1;
            x = (x * x) % p;
        }
        return res;

    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);

    }
    static long lgcd(long a, long b)
    {
        if (b == 0)
            return a;
        return lgcd(b, a % b);

    }
    public static void nsort(Node[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        // recursively sort
        nsort(a, low, mid);
        nsort(a, mid, high);
        // merge two sorted subarrays
        Node[] temp = new Node[N];
        int i = low, j = mid;

        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j].max<a[i].max)
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
	static class Node {
		  public int min;
		  public int max;

		  Node(int n,int c) {
		    min = n;
		    max = c;

		  }
		}
	static int MD= 998244353 ;
	static int N=1000000;
    public static void main(String[] args) throws IOException {
    	OutputStream outputStream = System.out;
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

	static class TaskC {

	    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
	    	int t=1;
	    	while(t-->0) {
	    		int n=in.nextInt();
	    		boolean[] asc=new boolean[n];
	    		int[][] c=new int[n][2];
    			long res=0;
	    		long cnt=0;
	    		for(int i=0;i<n;i++) {
	    			int m=in.nextInt();
	    			int x=in.nextInt();
	    			int mx=x;
	    			int mn=x;
	    			for(int j=1;j<m;j++) {
	    				x=in.nextInt();
	    				if(x>mn) {
	    					asc[i]=true;
	    					cnt++;
	    				}
	    				mx=Math.max(x, mx);
	    				mn=Math.min(x, mn);

	    			}
	    			c[i][0]=mn;
	    			c[i][1]=mx;
	    		}
	    		res+=cnt*(n);
	    		res+=cnt*(n-cnt);
	    		cnt=n-cnt;

	    	//	System.out.println(cnt);
	    		Node[] a=new Node[(int) cnt];
	    		int j=0;
	    		for(int i=0;i<n && j<cnt;i++) {
	    			if(asc[i]==false) {
	    				a[j]=new Node(c[i][0],c[i][1]);
	    				j++;
	    			}
	    		}

	    		nsort(a,0,(int)cnt);

	    		for(int i=0;i<cnt-1;i++) {
	    			int l=0,r=(int) cnt;
	    			while(l<r) {
	    				int m=l+(r-l)/2;
	    				if(a[m].max>a[i].min) {
	    					r=m;
	    				}
	    				else {
	    					l=m+1;
	    				}
	    			}
	    			res+=cnt-l;

	    		}
	    		out.println(res);

	        }
	    }

	}

    static long modexp(long x, long n)
    {
        if (n == 0) {
            return 1;
        }
        else if (n % 2 == 0) {
            return modexp((x * x) % MD, n / 2);
        }
        else {
            return (x * modexp((x * x) % MD, (n - 1) / 2) % MD);
        }
    }

     static long getInv(long b)
    {

        long  d = modexp(b, MD - 2);

        long ans =  (d % MD) ;

        return ans;
    }

    static class InputReader
    {
        BufferedReader br;
        StringTokenizer st;

        public InputReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }
    static void sort(int[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        sort(a, low, mid);
        sort(a, mid, high);
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    static void lsort(long[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        lsort(a, low, mid);
        lsort(a, mid, high);
        long[] temp = new long[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    public static long fexp(long x, long y, long p) {
        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;

            y = y >> 1;
            x = (x * x) % p;
        }
        return res;

    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);

    }
    static long lgcd(long a, long b)
    {
        if (b == 0)
            return a;
        return lgcd(b, a % b);

    }
    public static void nsort(Node[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        // recursively sort
        nsort(a, low, mid);
        nsort(a, mid, high);
        // merge two sorted subarrays
        Node[] temp = new Node[N];
        int i = low, j = mid;

        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j].max<a[i].max)
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
	static class Node {
		  public int min;
		  public int max;

		  Node(int n,int c) {
		    min = n;
		    max = c;

		  }
		}
	static int MD= 998244353 ;
	static int N=1000000;
    public static void main(String[] args) throws IOException {
    	OutputStream outputStream = System.out;
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

	static class TaskC {

	    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
	    	int t=1;
	    	while(t-->0) {
	    		int n=in.nextInt();
	    		boolean[] asc=new boolean[n];
	    		int[][] c=new int[n][2];
    			long res=0;
	    		long cnt=0;
	    		for(int i=0;i<n;i++) {
	    			int m=in.nextInt();
	    			int x=in.nextInt();
	    			int mx=x;
	    			int mn=x;
	    			for(int j=1;j<m;j++) {
	    				x=in.nextInt();
	    				if(x>mn) {
	    					asc[i]=true;
	    					cnt++;
	    				}
	    				mx=Math.max(x, mx);
	    				mn=Math.min(x, mn);

	    			}
	    			c[i][0]=mn;
	    			c[i][1]=mx;
	    		}
	    		res+=cnt*(n);
	    		res+=cnt*(n-cnt);
	    		cnt=n-cnt;

	    	//	System.out.println(res);
	    		Node[] a=new Node[(int) cnt];
	    		int j=0;
	    		for(int i=0;i<n ;i++) {
	    			if(asc[i]==false) {
	    				a[j]=new Node(c[i][0],c[i][1]);
	    				j++;
	    			}
	    		}

	    		nsort(a,0,(int)cnt);

	    		for(int i=0;i<cnt;i++) {
	    			int l=0,r=(int) cnt;
	    			while(l<r) {
	    				int m=l+(r-l)/2;
	    				if(a[m].max>a[i].min) {
	    					r=m;
	    				}
	    				else {
	    					l=m+1;
	    				}
	    		//		System.out.println(a[i].max+" l "+l);
	    			}

	    			res+=cnt-l;

	    		}
	    		out.println(res);

	        }
	    }

	}

    static long modexp(long x, long n)
    {
        if (n == 0) {
            return 1;
        }
        else if (n % 2 == 0) {
            return modexp((x * x) % MD, n / 2);
        }
        else {
            return (x * modexp((x * x) % MD, (n - 1) / 2) % MD);
        }
    }

     static long getInv(long b)
    {

        long  d = modexp(b, MD - 2);

        long ans =  (d % MD) ;

        return ans;
    }

    static class InputReader
    {
        BufferedReader br;
        StringTokenizer st;

        public InputReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }
    static void sort(int[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        sort(a, low, mid);
        sort(a, mid, high);
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    static void lsort(long[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        lsort(a, low, mid);
        lsort(a, mid, high);
        long[] temp = new long[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    public static long fexp(long x, long y, long p) {
        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;

            y = y >> 1;
            x = (x * x) % p;
        }
        return res;

    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);

    }
    static long lgcd(long a, long b)
    {
        if (b == 0)
            return a;
        return lgcd(b, a % b);

    }
    public static void nsort(Node[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        // recursively sort
        nsort(a, low, mid);
        nsort(a, mid, high);
        // merge two sorted subarrays
        Node[] temp = new Node[N];
        int i = low, j = mid;

        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j].max<a[i].max)
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
	static class Node {
		  public int min;
		  public int max;

		  Node(int n,int c) {
		    this.min = n;
		    this.max = c;

		  }
		}
	static int MD= 998244353 ;
	static int N=1000000;
    public static void main(String[] args) throws IOException {
    	OutputStream outputStream = System.out;
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

	static class TaskC {

	    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
	    	int t=1;
	    	while(t-->0) {
	    		int n=in.nextInt();
	    		boolean[] asc=new boolean[n];
	    		int[][] c=new int[n][2];
    			long res=0;
	    		long cnt=0;
	    		for(int i=0;i<n;i++) {
	    			int m=in.nextInt();
	    			int x=in.nextInt();
	    			int mx=x;
	    			int mn=x;
	    			boolean check=false;
	    			for(int j=1;j<m;j++) {
	    				x=in.nextInt();
	    				if(x>mn) {
	    					asc[i]=true;
	    					check=true;
	    				}
	    				mx=Math.max(x, mx);
	    				mn=Math.min(x, mn);

	    			}
	    			if(check) {
	    				cnt++;
	    			}
	    			c[i][0]=mn;
	    			c[i][1]=mx;
	    		}
	    		res+=cnt*(n);
	    		res+=cnt*(n-cnt);
	    		cnt=n-cnt;

	    	//	System.out.println(res);
	    		Node[] a=new Node[(int) cnt];
	    		int j=0;
	    		for(int i=0;i<n && j<cnt ;i++) {
	    			if(asc[i]==false) {
	    				a[j]=new Node(c[i][0],c[i][1]);
	    				j++;
	    			}
	    		}

	    		nsort(a,0,(int)cnt);

	    		for(int i=0;i<cnt;i++) {
	    			int l=0,r=(int) cnt;
	    			while(l<r) {
	    				int m=l+(r-l)/2;
	    				if(a[m].max>a[i].min) {
	    					r=m;
	    				}
	    				else {
	    					l=m+1;
	    				}
	    		//		System.out.println(a[i].max+" l "+l);
	    			}

	    			res+=cnt-l;

	    		}
	    		out.println(res);

	        }
	    }

	}

    static long modexp(long x, long n)
    {
        if (n == 0) {
            return 1;
        }
        else if (n % 2 == 0) {
            return modexp((x * x) % MD, n / 2);
        }
        else {
            return (x * modexp((x * x) % MD, (n - 1) / 2) % MD);
        }
    }

     static long getInv(long b)
    {

        long  d = modexp(b, MD - 2);

        long ans =  (d % MD) ;

        return ans;
    }

    static class InputReader
    {
        BufferedReader br;
        StringTokenizer st;

        public InputReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }
    static void sort(int[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        sort(a, low, mid);
        sort(a, mid, high);
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    static void lsort(long[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        lsort(a, low, mid);
        lsort(a, mid, high);
        long[] temp = new long[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j]<a[i])
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }
    public static long fexp(long x, long y, long p) {
        long res = 1;

        x = x % p;

        while (y > 0)
        {

            if((y & 1)==1)
                res = (res * x) % p;

            y = y >> 1;
            x = (x * x) % p;
        }
        return res;

    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);

    }
    static long lgcd(long a, long b)
    {
        if (b == 0)
            return a;
        return lgcd(b, a % b);

    }
    public static void nsort(Node[] a, int low, int high)
    {
        int N = high - low;
        if (N <= 1)
            return;
        int mid = low + N/2;
        // recursively sort
        nsort(a, low, mid);
        nsort(a, mid, high);
        // merge two sorted subarrays
        Node[] temp = new Node[N];
        int i = low, j = mid;

        for (int k = 0; k < N; k++)
        {
            if (i == mid)
                temp[k] = a[j++];
            else if (j == high)
                temp[k] = a[i++];
            else if (a[j].max<a[i].max)
                temp[k] = a[j++];
            else
                temp[k] = a[i++];
        }
        for (int k = 0; k < N; k++)
            a[low + k] = temp[k];
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class hello2
 {static class FastReader
       {
           BufferedReader br;
           StringTokenizer st;

           public FastReader()
           {
               br = new BufferedReader(new
                        InputStreamReader(System.in));
           }

           String next()
           {
               while (st == null || !st.hasMoreElements())
               {
                   try
                   {
                       st = new StringTokenizer(br.readLine());
                   }
                   catch (IOException  e)
                   {
                       e.printStackTrace();
                   }
               }
               return st.nextToken();
           }

           int nextInt()
           {
               return Integer.parseInt(next());
           }

           long nextLong()
           {
               return Long.parseLong(next());
           }

           double nextDouble()
           {
               return Double.parseDouble(next());
           }

           String nextLine()
           {
               String str = "";
               try
               {
                   str = br.readLine();
               }
               catch (IOException e)
               {
                   e.printStackTrace();
               }
               return str;
           }

       }

    static String sum (String s)
    {
        String s1 = "";

        if(s.contains("a"))
            s1+="a";
        if(s.contains("e"))
            s1+="e";
        if(s.contains("i"))
            s1+="i";
        if(s.contains("o"))
            s1+="o";
        if(s.contains("u"))
            s1+="u";

            return s1;

    }

    public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
    {
        // Create a list from elements of HashMap
        List<Map.Entry<String, Integer> > list =
               new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());

        // Sort the list
        Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2)
            {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // put data from sorted list to hashmap
        HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }

public static void main(String args[])
    {
	FastReader sc = new FastReader();     //For Fast IO
    int n = sc.nextInt();

    long min[] = new long[n];
    long max[] = new long[n];
    for(int i=0;i<n;i++){
        int l = sc.nextInt();
        int ma = Integer.MIN_VALUE;
        int mi = Integer.MAX_VALUE;
        int flag=0;
        for(int j=0;j<l;j++){
            int y = sc.nextInt();
            if(y>ma){
                ma=y;
            }
            if(y<=mi){
                mi=y;
            }
            else{
                flag=1;
            }

        }
        if(flag==1){                  //sequence is increasing.
           ma=1000000000;
           mi=-1000000000;
        }
        min[i]=mi;
        max[i]=ma;
    }

    Arrays.sort(max);
    long count=0;
    for(int i=0;i<n;i++){
          int index = upperBound(max,n,min[i]);
          count += (n-index);
    }
    System.out.println(count);
  }

public static int upperBound(long[] array, int length, long value) {
    int low = 0;
    int high = length;
    while (low < high) {
        final int mid = (low + high) / 2;
        if (value >= array[mid]) {
            low = mid + 1;
        } else {
            high = mid;
        }
}
return low;
}

static void transpose(char A[][], char B[][],int N)
{
    int i, j;
    for (i = 1; i <= N; i++)
        for (j = 1; j <= N; j++)
            B[i][j] = A[j][i];
}

public static boolean range(int x,int y,int n,int m)
{
	if(x<=0 || x>n || y<=0 || y>m)
		return false;

	return true;
}

public static int even_element(int b[],int n)
{
	int c[] = new int[n/2 + 1];
	int l=0;
	for(int i=1;i<n;i+=2)
	{
		c[l] = b[i];

		System.out.print(c[l] + " ");
		l++;
	}
	System.out.println();
	if(c.length!=1)
	{
		return even_element(c,n/2);
	}
	else
		return c[0];
}

    static boolean isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;

        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;

        return true;
    }
 }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class hello2
 {static class FastReader
       {
           BufferedReader br;
           StringTokenizer st;

           public FastReader()
           {
               br = new BufferedReader(new
                        InputStreamReader(System.in));
           }

           String next()
           {
               while (st == null || !st.hasMoreElements())
               {
                   try
                   {
                       st = new StringTokenizer(br.readLine());
                   }
                   catch (IOException  e)
                   {
                       e.printStackTrace();
                   }
               }
               return st.nextToken();
           }

           int nextInt()
           {
               return Integer.parseInt(next());
           }

           long nextLong()
           {
               return Long.parseLong(next());
           }

           double nextDouble()
           {
               return Double.parseDouble(next());
           }

           String nextLine()
           {
               String str = "";
               try
               {
                   str = br.readLine();
               }
               catch (IOException e)
               {
                   e.printStackTrace();
               }
               return str;
           }

       }

    static String sum (String s)
    {
        String s1 = "";

        if(s.contains("a"))
            s1+="a";
        if(s.contains("e"))
            s1+="e";
        if(s.contains("i"))
            s1+="i";
        if(s.contains("o"))
            s1+="o";
        if(s.contains("u"))
            s1+="u";

            return s1;

    }

    public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
    {
        // Create a list from elements of HashMap
        List<Map.Entry<String, Integer> > list =
               new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());

        // Sort the list
        Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2)
            {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // put data from sorted list to hashmap
        HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }

public static void main(String args[])
    {
        FastReader input =new FastReader();

        int t = input.nextInt();
        int m =0;
        int flag=0;
        ArrayList<Integer> a[] = new ArrayList[t+1];
        ArrayList<Integer> b[] = new ArrayList[t+1];
        int l=0;
        for(int i=0;i<t+1;i++)
    	{
    		a[i] = new ArrayList<Integer>();
    		b[i] = new ArrayList<Integer>();

    	}

        int j=0;

        while(j<t)
        {

        	int n = input.nextInt();

        	int min = Integer.MAX_VALUE;
        	int max = Integer.MIN_VALUE;

        	for(int i=0;i<n;i++)
        	{
        		int x = input.nextInt();
        		a[j].add(x);

        		if(min>a[j].get(i))
        		{
        			min = a[j].get(i);
        		}

        		if(max<a[j].get(i))
        		{
        			max = a[j].get(i);
        		}
        	}

        /*	for(int i=0;i<n;i++)
        	{
        		System.out.print(a[j].get(i) + " ");
        	}
        	System.out.println();
        	System.out.println(n);
        */
        	for(int k=0;k<n-1;k++)
        	{

        		if(a[j].get(k)<a[j].get(k+1))
        		{
        			flag=1;
        			m++;
        			break;
        		}
        		else
        			flag=0;

        	}

        	/*System.out.println(flag);
        	System.out.println();
        	*/if(flag==0)
        	{
        		for(int i=0;i<1;i++)
        		{
        			b[l].add(min);
        			b[l].add(max);
        		}
        		l++;
        	}

        	flag=0;
        	j++;
        }

        int k=0;
        int count =0;
        for(int i=0;i<l;i++)
        {

        	for(j=0;j<l;j++)
        	{
        		if(b[i].get(0)<b[j].get(1))
        		{
        			count++;
        		}
        	}

        }

     /*   for(int i = 0; i < l; i++){
            for( j = 0; j < b[i].size(); j++){
                System.out.print(b[i].get(j) + " ");
            }
            System.out.println();
        }

       */

        System.out.println(2*m*t - m*m + count);

   }
static void transpose(char A[][], char B[][],int N)
{
    int i, j;
    for (i = 1; i <= N; i++)
        for (j = 1; j <= N; j++)
            B[i][j] = A[j][i];
}

public static boolean range(int x,int y,int n,int m)
{
	if(x<=0 || x>n || y<=0 || y>m)
		return false;

	return true;
}

public static int even_element(int b[],int n)
{
	int c[] = new int[n/2 + 1];
	int l=0;
	for(int i=1;i<n;i+=2)
	{
		c[l] = b[i];

		System.out.print(c[l] + " ");
		l++;
	}
	System.out.println();
	if(c.length!=1)
	{
		return even_element(c,n/2);
	}
	else
		return c[0];
}

    static boolean isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;

        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;

        return true;
    }
 }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class hello2
 {static class FastReader
       {
           BufferedReader br;
           StringTokenizer st;

           public FastReader()
           {
               br = new BufferedReader(new
                        InputStreamReader(System.in));
           }

           String next()
           {
               while (st == null || !st.hasMoreElements())
               {
                   try
                   {
                       st = new StringTokenizer(br.readLine());
                   }
                   catch (IOException  e)
                   {
                       e.printStackTrace();
                   }
               }
               return st.nextToken();
           }

           int nextInt()
           {
               return Integer.parseInt(next());
           }

           long nextLong()
           {
               return Long.parseLong(next());
           }

           double nextDouble()
           {
               return Double.parseDouble(next());
           }

           String nextLine()
           {
               String str = "";
               try
               {
                   str = br.readLine();
               }
               catch (IOException e)
               {
                   e.printStackTrace();
               }
               return str;
           }

       }

    static String sum (String s)
    {
        String s1 = "";

        if(s.contains("a"))
            s1+="a";
        if(s.contains("e"))
            s1+="e";
        if(s.contains("i"))
            s1+="i";
        if(s.contains("o"))
            s1+="o";
        if(s.contains("u"))
            s1+="u";

            return s1;

    }

    public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
    {
        // Create a list from elements of HashMap
        List<Map.Entry<String, Integer> > list =
               new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());

        // Sort the list
        Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2)
            {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // put data from sorted list to hashmap
        HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }

public static void main(String args[])
    {
        FastReader input =new FastReader();

        int t = input.nextInt();
        long m =0;
        int flag=0;
        ArrayList<Integer> a[] = new ArrayList[t+1];
        ArrayList<Integer> b[] = new ArrayList[t+1];
        int l=0;
        for(int i=0;i<t+1;i++)
    	{
    		a[i] = new ArrayList<Integer>();
    		b[i] = new ArrayList<Integer>();

    	}

        int j=0;

        while(j<t)
        {

        	int n = input.nextInt();

        	int min = Integer.MAX_VALUE;
        	int max = Integer.MIN_VALUE;

        	for(int i=0;i<n;i++)
        	{
        		int x = input.nextInt();
        		a[j].add(x);

        		if(min>a[j].get(i))
        		{
        			min = a[j].get(i);
        		}

        		if(max<a[j].get(i))
        		{
        			max = a[j].get(i);
        		}
        	}

        /*	for(int i=0;i<n;i++)
        	{
        		System.out.print(a[j].get(i) + " ");
        	}
        	System.out.println();
        	System.out.println(n);
        */
        	for(int k=0;k<n-1;k++)
        	{

        		if(a[j].get(k)<a[j].get(k+1))
        		{
        			flag=1;
        			m++;
        			break;
        		}
        		else
        			flag=0;

        	}

        	/*System.out.println(flag);
        	System.out.println();
        	*/if(flag==0)
        	{
        		for(int i=0;i<1;i++)
        		{
        			b[l].add(min);
        			b[l].add(max);
        		}
        		l++;
        	}

        	flag=0;
        	j++;
        }

        int k=0;
        long count =0;
        for(int i=0;i<l;i++)
        {

        	for(j=0;j<l;j++)
        	{
        		if(b[i].get(0)<b[j].get(1))
        		{
        			count++;
        		}
        	}

        }

     /*   for(int i = 0; i < l; i++){
            for( j = 0; j < b[i].size(); j++){
                System.out.print(b[i].get(j) + " ");
            }
            System.out.println();
        }

       */

        System.out.println((long)2*m*t - (long)m*m + (long)count);

   }
static void transpose(char A[][], char B[][],int N)
{
    int i, j;
    for (i = 1; i <= N; i++)
        for (j = 1; j <= N; j++)
            B[i][j] = A[j][i];
}

public static boolean range(int x,int y,int n,int m)
{
	if(x<=0 || x>n || y<=0 || y>m)
		return false;

	return true;
}

public static int even_element(int b[],int n)
{
	int c[] = new int[n/2 + 1];
	int l=0;
	for(int i=1;i<n;i+=2)
	{
		c[l] = b[i];

		System.out.print(c[l] + " ");
		l++;
	}
	System.out.println();
	if(c.length!=1)
	{
		return even_element(c,n/2);
	}
	else
		return c[0];
}

    static boolean isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;

        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;

        return true;
    }
 }


import java.util.*;
import java.io.*;
public class HelloB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		long n = Integer.parseInt(bf.readLine());
 		long complementary = 0;
 		ArrayList<Sequence> array = new ArrayList<Sequence>();
 		for(int i = 0;i<n;i++){
 			StringTokenizer st = new StringTokenizer(bf.readLine());
 			int t = Integer.parseInt(st.nextToken());
 			int[] temp = new int[t];
 			boolean ascent = false;
 			for(int j = 0;j<t;j++){
 				temp[j] = Integer.parseInt(st.nextToken());
 				if (j>=1 && temp[j] > temp[j-1]){
 					ascent = true;
 					break;
 				}
 			}
 			if (ascent == false){
 				array.add(new Sequence(temp[0], temp[t-1]));
 			}
 		}
 		Collections.sort(array);
 		ArrayList<Integer> lefts = new ArrayList<Integer>();
 		for(Sequence s : array){
 			lefts.add(s.getLeft());
 		}
 		for (Sequence s : array){
 			complementary+=(lowerBoundS(lefts, s.getRight(), 0, lefts.size())+1);

 		}
 		out.println(n*n-complementary);

 		out.close();

 	}
	public static int lowerBoundS(ArrayList<Integer> array, int target, int l, int r){
		if (array.size() == 1)
			return 0;
		int mid = -1;
		while (l<r){
			mid = (l+r)/2;
			if (array.get(mid)<= target && (mid + 1 >= array.size() || array.get(mid+1) > target))
				return mid;
			else if (array.get(mid) <= target)
				l = mid+1;
			else
				r = mid;
		}
		return mid;

	}
}
class Sequence implements Comparable<Sequence>{
	private int left;
	private int right;
	public Sequence(int left, int right){
		this.left = left;
		this.right = right;
	}
	public int getLeft() {
		return left;
	}
	public void setLeft(int left) {
		this.left = left;
	}
	public int getRight() {
		return right;
	}
	public void setRight(int right) {
		this.right = right;
	}

	public int compareTo(Sequence other){
		if (left < other.left)
			return -1;
		else if (left > other.left)
			return 1;
		else
			return 0;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class HelloB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		long n = Integer.parseInt(bf.readLine());
 		long complementary = 0L;
 		ArrayList<Sequence> array = new ArrayList<Sequence>();
 		for(int i = 0;i<n;i++){
 			StringTokenizer st = new StringTokenizer(bf.readLine());
 			int t = Integer.parseInt(st.nextToken());
 			int[] temp = new int[t];
 			boolean ascent = false;
 			for(int j = 0;j<t;j++){
 				temp[j] = Integer.parseInt(st.nextToken());
 				if (j>=1 && temp[j] > temp[j-1]){
 					ascent = true;
 					break;
 				}
 			}
 			if (ascent == false){
 				array.add(new Sequence(temp[0], temp[t-1]));
 			}
 		}
 		Collections.sort(array);
 		ArrayList<Integer> lefts = new ArrayList<Integer>();
 		for(Sequence s : array){
 			lefts.add(s.getLeft());
 		}
 		for (Sequence s : array){
 			if (s.getRight() >= lefts.get(0))
 				complementary+=(long)((lowerBoundS(lefts, s.getRight(), 0, lefts.size())+1));

 		}
 		out.println(n*n-complementary);

 		out.close();

 	}
	public static int lowerBoundS(ArrayList<Integer> array, int target, int l, int r){
		if (array.size() == 1)
			return 0;
		int mid = -1;
		while (l<r){
			mid = (l+r)/2;
			if (array.get(mid)<= target && (mid + 1 >= array.size() || array.get(mid+1) > target))
				return mid;
			else if (array.get(mid) <= target)
				l = mid+1;
			else
				r = mid;
		}
		return mid;

	}
}
class Sequence implements Comparable<Sequence>{
	private int left;
	private int right;
	public Sequence(int left, int right){
		this.left = left;
		this.right = right;
	}
	public int getLeft() {
		return left;
	}
	public void setLeft(int left) {
		this.left = left;
	}
	public int getRight() {
		return right;
	}
	public void setRight(int right) {
		this.right = right;
	}

	public int compareTo(Sequence other){
		if (left < other.left)
			return -1;
		else if (left > other.left)
			return 1;
		else
			return 0;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class HelloB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		long n = Integer.parseInt(bf.readLine());
 		long complementary = 0;
 		ArrayList<Sequence> array = new ArrayList<Sequence>();
 		for(int i = 0;i<n;i++){
 			StringTokenizer st = new StringTokenizer(bf.readLine());
 			int t = Integer.parseInt(st.nextToken());
 			int[] temp = new int[t];
 			boolean ascent = false;
 			for(int j = 0;j<t;j++){
 				temp[j] = Integer.parseInt(st.nextToken());
 				if (j>=1 && temp[j] > temp[j-1]){
 					ascent = true;
 					break;
 				}
 			}
 			if (ascent == false){
 				array.add(new Sequence(temp[0], temp[t-1]));
 			}
 		}
 		Collections.sort(array);
 		ArrayList<Integer> lefts = new ArrayList<Integer>();
 		for(Sequence s : array){
 			lefts.add(s.getLeft());
 		}
 		for (Sequence s : array){
 			complementary+=(long)((lowerBoundS(lefts, s.getRight(), 0, lefts.size())+1));

 		}
 		out.println(n*n-complementary);

 		out.close();

 	}
	public static int lowerBoundS(ArrayList<Integer> array, int target, int l, int r){
		if (array.size() == 1)
			return 0;
		int mid = -1;
		while (l<r){
			mid = (l+r)/2;
			if (array.get(mid)<= target && (mid + 1 >= array.size() || array.get(mid+1) > target))
				return mid;
			else if (array.get(mid) <= target)
				l = mid+1;
			else
				r = mid;
		}
		return mid;

	}
}
class Sequence implements Comparable<Sequence>{
	private int left;
	private int right;
	public Sequence(int left, int right){
		this.left = left;
		this.right = right;
	}
	public int getLeft() {
		return left;
	}
	public void setLeft(int left) {
		this.left = left;
	}
	public int getRight() {
		return right;
	}
	public void setRight(int right) {
		this.right = right;
	}

	public int compareTo(Sequence other){
		if (left < other.left)
			return -1;
		else if (left > other.left)
			return 1;
		else
			return 0;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class HelloB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		long n = Integer.parseInt(bf.readLine());
 		long complementary = 0L;
 		ArrayList<Sequence> array = new ArrayList<Sequence>();
 		for(int i = 0;i<n;i++){
 			StringTokenizer st = new StringTokenizer(bf.readLine());
 			int t = Integer.parseInt(st.nextToken());
 			int[] temp = new int[t];
 			boolean ascent = false;
 			for(int j = 0;j<t;j++){
 				temp[j] = Integer.parseInt(st.nextToken());
 				if (j>=1 && temp[j] > temp[j-1]){
 					ascent = true;
 					break;
 				}
 			}
 			if (ascent == false){
 				array.add(new Sequence(temp[0], temp[t-1]));
 			}
 		}
 		Collections.sort(array);
 		ArrayList<Integer> lefts = new ArrayList<Integer>();
 		for(Sequence s : array){
 			lefts.add(s.getLeft());
 		}
 		for (Sequence s : array){
 			complementary+=(long)((lowerBoundS(lefts, s.getRight(), 0, lefts.size())+1));

 		}
 		out.println(n*n-complementary);

 		out.close();

 	}
	public static int lowerBoundS(ArrayList<Integer> array, int target, int l, int r){
		if (array.size() == 1)
			return 0;
		int mid = -1;
		while (l<r){
			mid = (l+r)/2;
			if (array.get(mid)<= target && (mid + 1 >= array.size() || array.get(mid+1) > target))
				return mid;
			else if (array.get(mid) <= target)
				l = mid+1;
			else
				r = mid;
		}
		return mid;

	}
}
class Sequence implements Comparable<Sequence>{
	private int left;
	private int right;
	public Sequence(int left, int right){
		this.left = left;
		this.right = right;
	}
	public int getLeft() {
		return left;
	}
	public void setLeft(int left) {
		this.left = left;
	}
	public int getRight() {
		return right;
	}
	public void setRight(int right) {
		this.right = right;
	}

	public int compareTo(Sequence other){
		if (left < other.left)
			return -1;
		else if (left > other.left)
			return 1;
		else
			return 0;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class HelloB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		int complementary = 0;
 		ArrayList<Sequence> array = new ArrayList<Sequence>();
 		for(int i = 0;i<n;i++){
 			StringTokenizer st = new StringTokenizer(bf.readLine());
 			int t = Integer.parseInt(st.nextToken());
 			int[] temp = new int[t];
 			boolean ascent = false;
 			for(int j = 0;j<t;j++){
 				temp[j] = Integer.parseInt(st.nextToken());
 				if (j>=1 && temp[j] > temp[j-1]){
 					ascent = true;
 					break;
 				}
 			}
 			if (ascent == false){
 				array.add(new Sequence(temp[0], temp[t-1]));
 			}
 		}
 		Collections.sort(array);
 		ArrayList<Integer> lefts = new ArrayList<Integer>();
 		for(Sequence s : array){
 			lefts.add(s.getLeft());
 		}
 		for (Sequence s : array){
 			complementary+=(lowerBoundS(lefts, s.getRight(), 0, lefts.size())+1);

 		}
 		out.println(n*n-complementary);

 		out.close();

 	}
	public static int lowerBoundS(ArrayList<Integer> array, int target, int l, int r){
		if (array.size() == 1)
			return 0;
		int mid = -1;
		while (l<r){
			mid = (l+r)/2;
			if (array.get(mid)<= target && (mid + 1 >= array.size() || array.get(mid+1) > target))
				return mid;
			else if (array.get(mid) <= target)
				l = mid+1;
			else
				r = mid;
		}
		return mid;

	}
}
class Sequence implements Comparable<Sequence>{
	private int left;
	private int right;
	public Sequence(int left, int right){
		this.left = left;
		this.right = right;
	}
	public int getLeft() {
		return left;
	}
	public void setLeft(int left) {
		this.left = left;
	}
	public int getRight() {
		return right;
	}
	public void setRight(int right) {
		this.right = right;
	}

	public int compareTo(Sequence other){
		if (left < other.left)
			return -1;
		else if (left > other.left)
			return 1;
		else
			return 0;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.io.*;
import java.util.StringTokenizer;

public class B_Rising {

    public static void main(String[] args) throws IOException {
        MyScanner in = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

        int n = in.nextInt();

        int[][] s = new int[n][2];
        int haveNotRising = 0;

        for (int i = 0; i < n; i++) {
            int m = in.nextInt();
            boolean isHasNotRising = true;
            int min = in.nextInt();
            int max = min;
            int j = 1;

            for (; j < m; j++) {
                int x = in.nextInt();
                if (x < min) {
                    min = x;
                } else if (x != min) {
                    isHasNotRising = false;
                    in.skipOtherElementsInLine();
                    break;
                }
            }

            if (isHasNotRising) {
                s[haveNotRising][0] = min;
                s[haveNotRising][1] = max;
                haveNotRising++;
            }
        }

        long result = 0;
        for (int i = 0; i < haveNotRising; i++) {
            if (s[i][0] < s[i][1]) {
                result++;
            }
            for (int j = i + 1; j < haveNotRising; j++) {
                if (s[i][0] < s[j][1]) {
                    result++;
                }
                if (s[j][0] < s[i][1]) {
                    result++;
                }
            }
        }

        int haveRising = n - haveNotRising;
        out.println(result + haveRising * haveRising + haveRising * haveNotRising * 2);

        out.close();
    }

    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        void skipOtherElementsInLine() throws IOException {
            st = null;
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}


import java.io.*;
import java.util.StringTokenizer;

public class B_Rising {

    public static void main(String[] args) throws IOException {
        MyScanner in = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

        int n = in.nextInt();

        int[][] s = new int[n][2];
        int haveNotRising = 0;

        for (int i = 0; i < n; i++) {
            int m = in.nextInt();
            boolean isHasNotRising = true;
            int min = in.nextInt();
            int max = min;
            int j = 1;

            for (; j < m; j++) {
                int x = in.nextInt();
                if (x < min) {
                    min = x;
                } else if (x != min) {
                    isHasNotRising = false;
                    in.skipOtherElementsInLine();
                    break;
                }
            }

            if (isHasNotRising) {
                s[haveNotRising][0] = min;
                s[haveNotRising][1] = max;
                haveNotRising++;
            }
        }

        long result = 0;
        for (int i = 0; i < haveNotRising; i++) {
            if (s[i][0] < s[i][1]) {
                result++;
            }
            for (int j = i + 1; j < haveNotRising; j++) {
                if (s[i][0] < s[j][1]) {
                    result++;
                }
                if (s[j][0] < s[i][1]) {
                    result++;
                }
            }
        }

        long haveRising = n - haveNotRising;
        out.println(result + haveRising * haveRising + haveRising * haveNotRising * 2);

        out.close();
    }

    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        void skipOtherElementsInLine() throws IOException {
            st = null;
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}