Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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()

Python: from sys import maxsize


def read_adj():
    n = int(input())
    adj_list = [[] for _ in range(n + 1)]
    for _ in range(1, n + 1):
        u, _, *adj = [int(x) for x in input().split()]
        adj_list[u] = adj
    return n, adj_list


def shortest_paths(n, adj_list, ini_key):
    dist = [maxsize] * (n + 1)

    S = [ini_key]
    dist[ini_key] = 0

    while S != []:
        key = S.pop()
        d = dist[key] + 1
        for k in adj_list[key]:
            if d < dist[k]:
                dist[k] = d
                S.append(k)

    return dist


def print_dist(n, dist):
    for i in range(1, n + 1):
        if dist[i] == maxsize:
            print(i, -1)
        else:
            print(i, dist[i])


def main():
    n, adj_list = read_adj()
    dist = shortest_paths(n, adj_list, 1)
    print_dist(n, dist)


if __name__ == '__main__':
    main()