Write a program which reads an directed graph $G = (V, E)$, and finds the shortest distance from vertex $1$ to each vertex (the number of edges in the shortest path). Vertices are identified by IDs $1, 2, ... n$.

Constraints

* $1 \leq n \leq 100$

Input

In the first line, an integer $n$ denoting the number of vertices, is given. In the next $n$ lines, adjacent lists of vertex $u$ are given in the following format:

$u$ $k$ $v_1$ $v_2$ ... $v_k$

$u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices adjacent to $u$.

Output

For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$ and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in order of IDs.

Example

Input

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


Output

1 0
2 1
3 2
4 1

//template code, C343, 2019
import java.util.LinkedList;
import java.util.Scanner;
import java.util.*;

class Graph {
	private int totalVertex;
	private LinkedList<LinkedList<Integer>> adjList;
	//adjacency list of edges
	public Graph() { totalVertex = 0; }
	public void loadAdjList() {
		Scanner in = new Scanner(System.in);
		totalVertex = in.nextInt();
		adjList = new LinkedList<LinkedList<Integer>>();
		for(int i = 0; i < totalVertex; i ++) {
			LinkedList<Integer> tmp = new LinkedList<Integer>();
			int idx1 = in.nextInt() - 1;
			int degree = in.nextInt();
			for(int j = 0; j < degree; j ++) {
				int idx2 = in.nextInt() - 1;
				tmp.add(idx2);
			}	
			adjList.add(tmp);
		}
		in.close();
	}
	
	public void bfs(Graph g, int source) {
        int[] dist = new int[g.length()]; //shortest path from source to v
        int[] previous = new int[g.length()]; //predecessor of v on the path
         
        //initializations
        for(int i = 0; i < g.length(); i ++) {
            dist[i] = Integer.MAX_VALUE;
            previous[i] = -1; //undefined
        }
        dist[source] = 0; //to itself
        ArrayList<Integer> unsettled = new ArrayList<Integer>(g.length());
        for(int i = 0; i < g.length(); i ++) unsettled.add(i);
         
        //iterate until all vertices "settled" or no more vertices can be reachable from the source
        while(unsettled.isEmpty() == false) {
            int u = getNearest(unsettled, dist); //u: vertex with minimal dist
            unsettled.remove(unsettled.indexOf(u)); //remove u from unsettled
            if(dist[u] == Double.MAX_VALUE) break; //remaining vertices not reachable from source
             
            LinkedList<Integer> neighbors = g.getNeighbors(u);
            for(int j = 0; j < neighbors.size(); j ++) {
                int v = neighbors.get(j);
                if(unsettled.contains(v) == true) {
                	//int weight = g.getWeight(u, v);
                    if(dist[u] + 1 < dist[v]) { //relax along u-v
                        dist[v] = dist[u] + 1;
                        previous[v] = u;
                    }
                }
            }
        }
        for(int i = 0; i < g.length(); i ++) {
            System.out.print(g.getVertex(i) + " ");
            if(dist[i] >= 2147483646 || dist[i] < 0) {
            	System.out.println("-1");
            }else {
            	System.out.println(dist[i]);
            }
        }
    }
    public int getNearest(ArrayList<Integer> vlist, int[] dist) {
        int v = vlist.get(0);
        double minDis = dist[v];
        for(int i = 1; i < vlist.size(); i ++) {
            if(dist[vlist.get(i)] < minDis) {
                v = vlist.get(i);
                minDis = dist[v];
            }
        }
        return v;
    }
	int length() { //int length(int index_of_node) {
//		int list_length = adjList.size();
//		if(index_of_node < list_length) {
//			return(adjList.get(index_of_node).size());
//		}else {
//			return -1;
//		}
		return adjList.size();
	}	
	
	LinkedList<Integer> getNeighbors(int index_of_node){
		LinkedList<Integer> neighbors = adjList.get(index_of_node);
		return neighbors;
	}
	int getVertex(int index_of_vertex) { //int getVertex(int index_of_node, int index_of_vertex) {
//		LinkedList<Integer> neighbors = getNeighbors(index_of_node);
//		int vertex = neighbors.get(index_of_vertex);
//		return vertex;
		return index_of_vertex + 1;
	}
}

//change class name GraphRepresentation to Main() for submission to AIZU
public class Main {
	public static void main(String argv[]) {
		Graph g = new Graph();
		g.loadAdjList();
		g.bfs(g, 0);
	}
}

