Problem: One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan.

A spider consists of k beads tied together by k - 1 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads.

Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features. 

After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible.

<image> <image>

The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture.

Input

The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next n lines contain the descriptions of each spider: integer ni (2 ≤ ni ≤ 100) — the number of beads, then ni - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to ni.

Output

Print a single number — the length of the required construction.

Examples

Input

1
3 1 2 2 3


Output

2


Input

2
3 1 2 1 3
4 1 2 2 3 2 4


Output

4


Input

2
5 1 2 2 3 3 4 3 5
7 3 4 1 2 2 4 4 6 2 7 6 5


Output

7

Solution (in JAVA):

Solution:
import java.io.Closeable;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Jacob Jiang
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream;
		try {
			inputStream = new FileInputStream("input.txt");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		OutputStream outputStream;
		try {
			outputStream = new FileOutputStream("output.txt");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		QuickScanner in = new QuickScanner(inputStream);
		ExtendedPrintWriter out = new ExtendedPrintWriter(outputStream);
		TaskF solver = new TaskF();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskF {
	public void solve(int testNumber, QuickScanner in, ExtendedPrintWriter out) {
        int treeCount = in.nextInt();
        int answer = 0;
        for (int treeNo = 0; treeNo < treeCount; treeNo++) {
            int n = in.nextInt();
            int[] from = new int[n - 1];
            int[] to = new int[n - 1];
            ArrayUtils.readIntArrays(in, from, to);
            ArrayUtils.decreaseByOne(from, to);
            answer += getMaxLength(new FixedTree(n, from, to));
        }
        out.println(answer);
	}

    private int getMaxLength(Tree tree) {
        int n = tree.getVertexCount();
        int[] depth = new int[n];
        for (int i : tree.getLevelOrder()) {
            int parent = tree.getParent(i);
            depth[i] = parent == -1 ? 0 : depth[parent] + 1;
        }
        int maxIndex = ArrayUtils.maxElement(depth);
        tree.setRoot(maxIndex);
        for (int i : tree.getLevelOrder()) {
            int parent = tree.getParent(i);
            depth[i] = parent == -1 ? 0 : depth[parent] + 1;
        }
        return ArrayUtils.max(depth);
    }
}

class QuickScanner implements Iterator<String>, Closeable {
    BufferedReader reader;
    StringTokenizer tokenizer;
    boolean endOfFile = false;

    public QuickScanner(InputStream inputStream){
        reader = new BufferedReader(new InputStreamReader(inputStream));
        tokenizer = null;
    }

    public boolean hasNext() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                checkNext();
            } catch (NoSuchElementException ignored) {
            }

        }
        return !endOfFile;
    }

    private void checkNext() {
        if (endOfFile) {
            throw new NoSuchElementException();
        }
        try {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
        } catch (Exception e) {
            endOfFile = true;
            throw new NoSuchElementException();
        }
    }

    public String next() {
        checkNext();
        return tokenizer.nextToken();
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

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

    public void close() {
        try {
            reader.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}

class ExtendedPrintWriter extends PrintWriter {


    public ExtendedPrintWriter(Writer out) {
        super(out);
    }

    public ExtendedPrintWriter(OutputStream out) {
        super(out);
    }

    }

class ArrayUtils {

    private ArrayUtils() {
    }

    public static int max(int[] array) {
        if (array.length == 0) {
            throw new IllegalArgumentException();
        }
        int result = array[0];
        for (int i = 1; i < array.length; i++) {
            result = Math.max(result, array[i]);
        }
        return result;
    }

    public static int maxElement(int[] array) {
        int maxNumber = max(array);
        for (int i = 0; i < array.length; i++) {
            if (array[i] == maxNumber) {
                return i;
            }
        }
        return -1;
    }

    public static void readIntArrays(QuickScanner in, int[]... arrays) {
        for (int i = 0; i < arrays[0].length; i++) {
            for (int j = 0; j < arrays.length; j++) {
                arrays[j][i] = in.nextInt();
            }
        }
    }

    public static void decreaseByOne(int[]... arrays) {
        for (int[] array : arrays) {
            for (int i = 0; i < array.length; i++) {
                array[i]--;
            }
        }
    }

    }

interface Tree extends Graph {
    public int getParent(int vertexNo);
    public void setRoot(int root);
    public int[] getLevelOrder();
    }

class FixedTree extends BidirectionalFixedGraph implements Tree {
    protected int[] parent;
    protected int[] order;
    protected int root;

    public FixedTree(int vertexCount, int[] from, int[] to) {
        this(vertexCount, from, to, 0);
    }

    public FixedTree(int vertexCount, int[] from, int[] to, int root) {
        super(vertexCount, from, to);
        if (from.length != vertexCount - 1) {
            throw new IllegalArgumentException();
        }
        parent = new int[getVertexCount()];
        order = new int[getVertexCount()];
        setRoot(root);
    }

    public int getParent(int vertexNo) {
        return parent[vertexNo];
    }

    public void setRoot(int root) {
        this.root = root;
        int orderIndex = 0;
        order[orderIndex++] = root;
        parent[root] = -1;
        for (int i = 0; i < getVertexCount(); i++) {
            int current = order[i];
            for (int child : edges[current]) {
                if (child != parent[current]) {
                    order[orderIndex++] = child;
                    parent[child] = current;
                }
            }
        }
    }

    public int[] getLevelOrder() {
        return order;
    }

    public int getDegree(int vertexNo) {
        if (vertexNo == root) {
            return super.getDegree(vertexNo);
        } else {
            return super.getDegree(vertexNo) - 1;
        }
    }

    public int[] getConnectedEdgesArray(int vertexNo) {
        int[] temp = super.getConnectedEdgesArray(vertexNo);
        int[] result = new int[getDegree(vertexNo)];
        int index = 0;
        for (int item : temp) {
            if (item != getParent(vertexNo)) {
                result[index++] = item;
            }
        }
        return result;
    }
}

interface Graph {
    int getVertexCount();
    }

class BidirectionalFixedGraph extends FixedGraph {
    public BidirectionalFixedGraph(int vertexCount, int[] from, int[] to) {
        if (from.length != to.length) {
            throw new IllegalArgumentException();
        }
        edges = new int[vertexCount][];
        int[] degree = new int[vertexCount];
        for (int i = 0; i < from.length; i++) {
            degree[from[i]]++;
            degree[to[i]]++;
        }
        for (int i = 0; i < vertexCount; i++) {
            edges[i] = new int[degree[i]];
        }
        for (int i = 0; i < from.length; i++) {
            edges[from[i]][--degree[from[i]]] = to[i];
            edges[to[i]][--degree[to[i]]] = from[i];
        }
    }
}

abstract class FixedGraph implements Graph {
    protected int[][] edges;

    public int getVertexCount() {
        return edges.length;
    }

    public int getDegree(int vertexNo) {
        return edges[vertexNo].length;
    }

    public int[] getConnectedEdgesArray(int vertexNo) {
        return edges[vertexNo];
    }

    }