Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web.

<image>

There are n main threads going from the center of the web. All main threads are located in one plane and divide it into n equal infinite sectors. The sectors are indexed from 1 to n in the clockwise direction. Sectors i and i + 1 are adjacent for every i, 1 ≤ i < n. In addition, sectors 1 and n are also adjacent.

Some sectors have bridge threads. Each bridge connects the two main threads that make up this sector. The points at which the bridge is attached to the main threads will be called attachment points. Both attachment points of a bridge are at the same distance from the center of the web. At each attachment point exactly one bridge is attached. The bridges are adjacent if they are in the same sector, and there are no other bridges between them.

A cell of the web is a trapezoid, which is located in one of the sectors and is bounded by two main threads and two adjacent bridges. You can see that the sides of the cell may have the attachment points of bridges from adjacent sectors. If the number of attachment points on one side of the cell is not equal to the number of attachment points on the other side, it creates an imbalance of pulling forces on this cell and this may eventually destroy the entire web. We'll call such a cell unstable. The perfect web does not contain unstable cells.

Unstable cells are marked red in the figure. Stable cells are marked green.

Paw the Spider isn't a skillful webmaker yet, he is only learning to make perfect webs. Help Paw to determine the number of unstable cells in the web he has just spun.

Input

The first line contains integer n (3 ≤ n ≤ 1000) — the number of main threads.

The i-th of following n lines describe the bridges located in the i-th sector: first it contains integer ki (1 ≤ ki ≤ 105) equal to the number of bridges in the given sector. Then follow ki different integers pij (1 ≤ pij ≤ 105; 1 ≤ j ≤ ki). Number pij equals the distance from the attachment points of the j-th bridge of the i-th sector to the center of the web.

It is guaranteed that any two bridges between adjacent sectors are attached at a different distance from the center of the web. It is guaranteed that the total number of the bridges doesn't exceed 105.

Output

Print a single integer — the number of unstable cells in Paw the Spider's web.

Examples

Input

7
3 1 6 7
4 3 5 2 9
2 8 1
4 3 7 6 4
3 2 5 9
3 6 3 8
3 4 2 9


Output

6
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author cunbidun
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        Task216D solver = new Task216D();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task216D {
        private InputReader in;
        private OutputWriter out;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            this.in = in;
            this.out = out;
            int n = in.nextInt();
            ArrayList<Integer> list = new ArrayList<>();
            int[][] thread = new int[n + 1][];
            TreeSet<Integer> set = new TreeSet<>();

            int cnt = 0;

            for (int i = 1; i <= n; i++) {
                int k = in.nextInt();
                thread[i] = new int[k + 1];
                thread[i][0] = k;
                for (int j = 1; j <= k; j++) {
                    int x = in.nextInt();
                    thread[i][j] = x;
                    if (!set.contains(x)) {
                        set.add(x);
                        list.add(x);
                        cnt++;
                    }
                }
                QuickSort.sort(thread[i], 1, k);
            }

            int[] pos = new int[(int) 1e5 + 1];
            Collections.sort(list);
            for (int i = 0; i < list.size(); i++) {
                pos[list.get(i)] = i + 1;
            }

            set.clear();
            list.clear();

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= thread[i][0]; j++) {
                    thread[i][j] = pos[thread[i][j]];
                }
            }

            FTSum[] ft = new FTSum[n + 1];
            ft[n] = new FTSum(MaxMin.Max(thread[n][thread[n][0]], thread[1][thread[1][0]]));

            for (int j = 1; j <= thread[1][0]; j++) {
                ft[n].update(thread[1][j], 1);
            }
            for (int j = 1; j <= thread[n][0]; j++) {
                ft[n].update(thread[n][j], 1);
            }
            int ans = 0;

            for (int i = 1; i < n; i++) {
                ft[i] = new FTSum(MaxMin.Max(thread[i][thread[i][0]], thread[i + 1][thread[i + 1][0]]));
                for (int j = 1; j <= thread[i][0]; j++) {
                    ft[i].update(thread[i][j], 1);
                }
                for (int j = 1; j <= thread[i + 1][0]; j++) {
                    ft[i].update(thread[i + 1][j], 1);
                }
                if (i > 1) {
                    for (int j = 2; j <= thread[i][0]; j++) {
                        if (ft[i - 1].getSum(thread[i][j - 1], thread[i][j]) != ft[i].getSum(thread[i][j - 1], thread[i][j]))
                            ans++;
                    }
                    if (i > 2) ft[i - 1].clear();
                }
            }

            for (int j = 2; j <= thread[1][0]; j++) {
                if (ft[1].getSum(thread[1][j - 1], thread[1][j]) != ft[n].getSum(thread[1][j - 1], thread[1][j]))
                    ans++;
            }
            for (int j = 2; j <= thread[n][0]; j++) {
                if (ft[n - 1].getSum(thread[n][j - 1], thread[n][j]) != ft[n].getSum(thread[n][j - 1], thread[n][j]))
                    ans++;
            }
//        for (int i = 2; i <= n; i++) {
//            for (int j = 2; j <= thread[i][0]; j++) {
//                if (ft[i - 1].getSum(thread[i][j - 1], thread[i][j]) != ft[i].getSum(thread[i][j - 1], thread[i][j]))
//                    ans++;
//            }
//        }
            out.println(ans);
        }

    }

    static class QuickSort {
        public static void sort(int[] list, int lowIndex, int highIndex) {
            ArrayUtils.shuffleArray(list, lowIndex, highIndex);
            Arrays.sort(list, lowIndex, highIndex + 1);
        }

    }

    static class OutputWriter {
        private final PrintWriter out;

        public OutputWriter(OutputStream outputStream) {
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.out = new PrintWriter(writer);
        }

        public void close() {
            out.close();
        }

        public void println(int i) {
            out.println(i);
        }

    }

    static class InputReader extends InputStream {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class FTSum {
        private int n;
        private int[] sum;

        public FTSum(int n) {
            sum = new int[n + 1];
            this.n = n;
        }

        public void update(int pos, int val) {
            while (pos <= n) {
                sum[pos] += val;
                pos += pos & -pos;
            }
        }

        public int getSum(int pos) {
            int ans = 0;
            while (pos != 0) {
                ans += sum[pos];
                pos -= pos & -pos;
            }
            return ans;
        }

        public int getSum(int l, int r) {
            return getSum(r) - getSum(l - 1);
        }

        public void clear() {
            sum = null;
            n = 0;
        }

    }

    static class ArrayUtils {
        public static void shuffleArray(int[] array, int stIndex, int enIndex) {
            for (int i = enIndex; i > stIndex; i--) {
                int index = (int) OneNumberGen.genInt(stIndex, i - 1);
                if (index != i) {
                    array[index] ^= array[i];
                    array[i] ^= array[index];
                    array[index] ^= array[i];
                }
            }
        }

    }

    static class OneNumberGen {
        private static long genI(long min, long max) {
            return ThreadLocalRandom.current().nextLong(min, max + 1);
        }

        public static long genInt(long min, long max) {
            if (min >= 0)
                return (genI(min, max));
            else if (max >= 0) {
                double ratio = (double) Math.abs(min) / (Math.abs(min) + max);
                ratio *= 1000000000000000000D;
                double prob = (double) (OneNumberGen.genInt(1, 1000000000000000000L));
                if (Double.compare(ratio, prob) > 0) {
                    return -(genI(0, -min));
                } else {
                    return (genI(0, max));
                }
            } else
                return -(OneNumberGen.genI(-min, -max));
        }

    }

    static class MaxMin {
        public static <T extends Comparable<T>> T Max(T x, T y) {
            T max = x;
            if (y.compareTo(max) > 0) max = y;
            return max;
        }

    }
}

