Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

N = int(input())
neighborlist = [None for _ in range(N+1)]
for _ in range(1, N+1):
    node, _, *L = list(map(int, input().split()))
    neighborlist[node] = L

distancelist = [-1]*(N+1)
queue = deque()
queue.append(1)
distancelist[1] = 0
while queue:
    node = queue.popleft()
    distance = distancelist[node]
    for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor)

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

Python: from collections import deque
V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v)
dist = [-1] * V
dist[0] = 0
que = deque([0])

while len(que):
    v = que.popleft()
    for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c)
for i, d in enumerate(dist):
    print(i+1, d)

Python: from collections import deque

def bfs(s, n, m):
    q = deque()
    q.append(s)
    INFTY = 1 << 21
    d = [INFTY] * n
    d[s] = 0
    while len(q) != 0:
        u = q[0]
        q.popleft()
        for v in range(n):
            if not m[u][v]:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.append(v)
    for i in range(n):
        result = str(i+1)
        if d[i] == INFTY:
            result += ' -1'
        else:
            result += ' %d' % d[i]
        print(result)


n = int(input())
m = [[False] * n for i in range(n)]
for i in range(n):
    line = [int(v) for v in input().split()]
    u = line[0] - 1
    for v in line[2:]:
        v -= 1
        m[u][v] = True

bfs(0, n, m)

Python: import queue

N = int(input())
G = []
for _ in range(N):
    u, n, *K = map(int, input().split())
    G.append((u - 1, [k - 1 for k in K]))

Q = queue.Queue()
Q.put(G[0])
dist = [-1] * N
dist[0] = 0
while not Q.empty():
    U = Q.get()
    for u in U[1]:
        if dist[u] == -1:
            Q.put(G[u])
            dist[u] = dist[U[0]] + 1

for i, time in enumerate(dist):
    print(i + 1, time)

Python: import sys

n = int(sys.stdin.readline())
dist = [0] + [-1] * (n-1)
vv=[list(map(int, sys.stdin.readline().split()))[2:] for _ in range(n)]


def walk(k, d):
    for i in vv[k]:
        if dist[i-1]<0 or (dist[i-1]>0 and dist[i-1]>d):
            dist[i-1]=d
        else: continue
        walk(i-1, d+1)

walk(0, 1)

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

Python: from collections import deque
def dfs(s):
    q.append(s)
    d = [INF] * n
    d[s] = 0
    while len(q):
        u = q.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            q.append(v)
    for i in range(n):
        if d[i] == INF:
            d[i] = -1
        print('{} {}'.format(i+1, d[i]))
n = int(input())
data = [list(map(int, input().split()))for _ in range(n)]
#print(data)
color = []
INF = 1000000000000000000000000000000000000000
M = [[0 for _ in range(n)]for _ in range(n)]
q = deque([])
for i in data:
    for j in i[2:]:
        M[i[0]-1][j-1] = 1
dfs(0)

Python: from sys import stdin
from collections import deque
n = int(stdin.readline())
d = [-1] * (n + 1)
def bfs(G):
    d[1] = 0
    queue = [1]
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x)
G = {}
for i in range(n):
    x = list(map(int, stdin.readline().split()))
    G[x[0]] = x[2:]
bfs(G)

Python: from collections import deque

n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0]
    num = input_list[1]

    if num >= 1:
        nodes = input_list[2:]
        for node in nodes:
            graph[index].append(node)
            # graph[node].append(index)

visited = [-1] * (n + 1)
num = [-1] * (n + 1)
q = deque()
visited[1] = 1
num[1] = 0
q.append(1)

while q:
    index = q.popleft()
    
    for node in graph[index]:
        if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node)


for i, j in enumerate(num):
    if i == 0:
        continue
    print(i, j)

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
frag, T, L,Q = [0]*N, [0]*N, [0]*N, [0]
frag[0] = 1
def func(ide):
    for i in E[ide]:
        i = int(i) - 1
        if frag[i] != 1:frag[i] = 1;L[i] = L[ide] +1 ;Q.append(i) 
        else:return

while Q:top = Q[0];func(top);del Q[0]
    
for i in range(1,N):
    if int(L[i]) == 0:
        L[i] = -1

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

Python: #from collections import deque
import sys

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

#def mypopleft(que, index):
#    if len(que) > index:
#        return que.pop(index)
#    else:
#        return []


def main():
    # input
    n = int(input())

    graph = []
    for i in range(n):
        u, k, *tmp = [int(i) for i in readline().split()]
        graph.append(tmp)
    
    #print(graph)
    d = [-1]*n
    #que = deque([1])
    que = [1]
    #count = 0
    d[0] = 0
    check = []

    while True:
        v = que.pop(0)
        #print(v)
        #count += 1
        #print(d)
        for u in graph[v-1]:
            #print(u)
            if d[u-1] == -1:
                d[u-1] = d[v-1] + 1
                que.append(u)
        if len(que) == 0:
            break
    
    for i in range(n):
        print("{} {}".format(i+1, d[i]))


if __name__ == "__main__":
    main()