Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

Python: import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect

# from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())
adj = [list(map(int, input().split()))[2:] for _ in range(n)]

d = [-1] * n

for a in adj:
    for i in range(len(a)):
        a[i] -= 1

s = 0
q = collections.deque([])
d[s] = 0
q.append(s)
while len(q):
    u = q.popleft()
    v = adj[u]
    for vi in v:
        if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi)

for i, di in enumerate(d):
    print(i + 1, di)

Python: n=int(input())
G=[[] for i in range(n)]
#G=[]
Q=[1]
d=[-1]*n
d[0]=0
visited=[False]*n
visited[0]=True

for i in range(n):
    arr=[int(x) for x in input().split()[2:]]
    for l in arr:
        G[i].append(l)
        #G[l-1].append(i+1)
    

kyori=0
while len(Q)!=0:
    
    #print(Q)
    #print(visited)
    v=Q.pop(0)
    
    for j in G[v-1]:
        if visited[j-1]:#次に行くところがすでに踏破済みだったら
            continue#更新しない
        else:
            d[j-1]=d[v-1]+1
            Q.append(j)
            visited[j-1]=True
for a in range(n):
    print(a+1,d[a])

Python: from sys import stdin
from collections import deque
n = int(stdin.readline())
M = [[0] * (n + 1) for _ in range(n + 1)]
d = [200] * (n + 1)
def bfs():
    d[1] = 0
    dq = deque()
    dq.append(1)
    while len(dq) > 0:
        u = dq.popleft()
        for v in range(1, n + 1):
            if M[u][v] == 0 or d[v] != 200: continue
            d[v] = d[u] + 1
            dq.append(v)
    for i in range(1, n + 1):
        print(i, (-1 if d[i] == 200 else d[i]))
for u in range(1, n + 1):
    U = [int(j) for j in stdin.readline().split()]
    for v in U[2:]:
        M[u][v] = 1
bfs()

Python: n = int(input())
adj = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
    l = list(map(int, input().split()))
    if l[1] == 0:
        continue 
    else:
        for j in range(2,len(l)):
            adj[l[0]-1][l[j]-1] = 1

color = ['white' for _ in range(n)]
d = [-1 for _ in range(n)]
color[0] = 'grey'
d[0] = 0
Q = [1]
while Q:
    u = Q.pop(0)
    color[u-1] = 'black'
    for v in range(1, n+1):
        if adj[u-1][v-1] == 1 and color[v-1] == 'white':
            Q.append(v)
            color[v-1] = 'grey'
            d[v-1] = d[u-1] + 1  

for i in range(n):
    print(' '.join([str(i+1), str(d[i])]))

Python: n = int(input())

m = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    com = list(map(int, input().split()))
    for j in range(com[1]):
        m[i][com[j+2]-1] = 1

color = ["white" for i in range(n)]

from collections import deque
queue = deque()

d = [-1 for i in range(n)]

def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black"

bfs()
for i in range(n):
    print(i+1, d[i])

Python: import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)]

D = [-1]*N
q = deque([[0, 0]])
while q:
    cur, dis = q.popleft()
    if D[cur] != -1: continue
    D[cur] = dis
    for nex in G[cur]:
        q.append([nex,dis+1])
for i in range(N):
    print(i+1, D[i])

Python: # -*- coding: utf-8 -*-
"""
Created on Sat Apr  6 11:50:13 2019

@author: Yamazaki Kenichi
"""
from collections import deque

n = int(input())
A = [[]] + [list(map(int,input().split())) for i in range(n)]

#B = [[0 for i in range(n+1)] for i in range(n+1)]
#for i in range(1,n+1):
#    for j in range(A[i][1]):
#        B[i][A[i][j+2]] = 1

T = [[-1, -1] for i in range(n+1)]
T[1][0], T[1][1] = 1, 0
q = deque([1])
def dfs():
    global depth
    while len(q) > 0:
        for i in range(A[q[0]][1]):
            if T[A[q[0]][i+2]][0] == -1:
                q.append(A[q[0]][i+2])
                T[q[-1]][0] = 1
                T[q[-1]][1] = T[q[0]][1] + 1
        q.popleft()
dfs()
for i in range(1,n+1):
    print(i,T[i][1])

Python: from collections import deque
n = int(input())
data = [None]*n
for _ in range(n):
    a,*b = map(int,input().split())
    a -= 1
    data[a] = [i-1 for i in b[1:]]
d = [-1]*n
dq = deque([(0,0)])
while dq:
    p,q = dq.popleft()
    if d[p] == -1:
        d[p] = q
        for i in data[p]:
            dq.append((i,q+1))
for i,j in enumerate(d):
    print(i+1,j)

Python: N=int(input())
M=[input().split()[2:]for _ in[0]*N]
q=[0]
d=[0]+[-1]*N
while q:
 u=q.pop(0)
 for v in M[u]:
  v=int(v)-1
  if d[v]<0:d[v]=d[u]+1;q+=[v]
for i in range(N):print(i+1,d[i])

Python: N=int(input())
M=[input().split()[2:]for _ in[0]*N]
q=[0]
d=[0]+[-1]*N
while q:
 u=q.pop(0)
 for v in M[u]:
  v=int(v)-1
  if d[v]<0:d[v]=d[u]+1;q+=[v]
for i in range(N):print(i+1,d[i])

Python: n=int(input())
dic=[[] for i in range(n)]
for i in range(n):
    a=list(map(int,input().split()))
    a=a[2:]
    dic[i]=list(map(lambda x:x-1,a))

from collections import deque
d=deque()
d.append(0) #始点
completed=[False]*n
distance=[999]*n
distance[0]=0
while d:
    now=d.popleft()
    completed[now]=True
    for x in dic[now]: #次の頂点探し
        if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x)
for i in range(n):
    if distance[i]==999:
        distance[i]=-1
    print(i+1,distance[i])