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: from collections import deque

def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	
n=int(input())
L=[]
for i in range(n):
	A=[]
	A=list(map(int,input().split()))
	L.append(A[2:])
v=[-1]*n
bfs()
for i in range(n):
	print(i+1,v[i])

Python: INFTY = 10 ** 10

import queue

def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i])

n = int(input())
M = [[0] * n for _ in range(n)]

for i in range(n):
    nums = list(map(int, input().split()))
    u = nums[0] - 1
    k = nums[1]
    for j in range(k):
        v = nums[j+2] - 1
        M[u][v] = 1

bfs(0)

Python: import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


N = int(input())

edge = {}
for _ in range(N):
    tmp = inpl()
    u, k = tmp[:2]
    edge[u] = tmp[2:]

d = {i: -1 for i in range(1, N + 1)}
d[1] = 0
Q = deque()
Q.append(1)
while Q:
    v = Q.popleft()
    for nv in edge[v]:
        if d[nv] != -1:
            continue
        d[nv] = d[v] + 1
        Q.append(nv)

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

Python: from collections import deque


n = int(input())
g_list = {}
for _ in range(n):
    n1, num, *cl = map(int, input().split())
    g_list[n1] = cl

# 書けないから移す。
GRAY = 1
WHITE = 0
BLACK = 2


class Graph():
    def __init__(self, g_list):
        self.g_list = g_list
        self.dst = [-1] * (len(g_list)+1)
        self.stack = deque([])
        self.colors = [WHITE] * (len(g_list)+1)

    def bfs(self, s):
        self.colors[s] = GRAY
        self.dst[s] = 0
        self.stack.append(s)

        while self.stack:
            u = self.stack.popleft()
            for v in self.g_list[u]:
                if self.colors[v] == WHITE:
                    self.colors[v] = GRAY
                    self.dst[v] = self.dst[u] + 1
                    self.stack.append(v)
            self.colors[u] = BLACK


g = Graph(g_list)
g.bfs(1)
for i in range(1, n+1):
    print(i, g.dst[i])

Python: import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
g = [None]*n
for i in range(n):
    g[i] = readints()
# print(g)
G = [None]*n
for i in range(n):
    G[i] = []
# print(G)
for i in range(n):
    if len(g[i]) >= 3:
        for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G)
dist = [-1]*n
dist[0] = 0
d = deque()
d.append(0)
# print(d)
while(len(d) != 0):
    v = d.popleft()
    if G[v] != []:
        for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1)
for i in range(n):
    print(i+1, dist[i])

Python: # -*- coding: utf-8 -*-
from collections import deque


if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

    color = [0] * n
    d = [float("inf")] * n
    Q = deque()

    def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 0
        Q.append(s)

        while len(Q) != 0:
            u = Q.popleft()
            for v in range(n):
                if M[u][v] == 1 and color[v] == 0:
                    color[v] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

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

Python: n=int(input())
G=[[0,[],0,-1]]
for i in range(n):
    v=[int(j) for j in input().split(" ")]
    u=v.pop(0)
    k=v.pop(0)
    G.append([k,v,0,-1])





distance=0
dis_list=[1]
G[1][3]=0
G[1][2]=1
while len(dis_list)!=0:
    distance+=1
    dis_list_cash=[]
    for i in dis_list:
        k=G[i][0]
        v=G[i][1]
        for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
    dis_list=dis_list_cash



for i in range(1,n+1):
    print(str(i)+" "+str(G[i][3]))

Python: n = int(input())
E = [[]]*n

for i in range(n):
    inputs = list(map(int, input().split()))
    if inputs[1]>0:
        for e in inputs[2:]:
            E[i] = E[i] + [e-1]

visited = [-1]*n
dist = [-1]*n
dist[0] = 0

from collections import deque

d = deque()
d.append(0)
visited[0] = 1

while len(d):
    nd = d.popleft()
    #print(nd, E[nd])
    for e in E[nd]:
        #print(e)
        if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
            

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