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: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

n = int(input())
dis = [-1] * n
info = [list(map(int, input().split())) for _ in range(n)]

stack = [[0, 0]]
ans = 0
while len(stack) > 0:
    num = stack.pop(0)
    if dis[num[0]] == -1:
        dis[num[0]] = num[1]
    for i in info[num[0]][2:]:
        if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1])
for i in range(n):
    print(str(i+1) + " " + str(dis[i]))

Python: n=int(input())
ne=[[] for _ in range(n+1)]
for _ in range(n):
    u,k,*v=map(int,input().split())
    ne[u]=v
dis=[-1]*(n+1)
dis[1]=0
q=[1]
while q:
    p=q.pop(0)
    for e in ne[p]:
        if dis[e]==-1:
            dis[e]=dis[p]+1
            q.append(e)
for i in range(1,n+1):
    print(i,dis[i])

Python: from collections import deque
Q=deque()
n=int(input())
graph={}
d=[0 for i in range(n+1)]

for i in range(n):
    tmp=list(map(int,input().split()))
    graph[tmp[0]]=[0 for j in range(n+1)]
    for j in range(tmp[1]):
        graph[tmp[0]][tmp[2+j]]=1

Q.append(1)
while len(Q) is not 0:
    number=Q[0]
    if 1 in graph[number]:
        for i in range(graph[number].count(1)):
            Q.append(graph[number].index(1))
            if d[graph[number].index(1)] is 0:
                d[graph[number].index(1)]=d[number]+1
            graph[number][graph[number].index(1)]=0
        
    Q.popleft()

for i in range(n):
    if i==0:
        print(i+1,0)
    else:
        if d[i+1]==0:
            print(i+1,-1)
        else:
            print(i+1,d[i+1])

Python: def srch(i, l, Lng, Dst):
    if Lng[i] > l:
        Lng[i] = l

    if Dst[i] is not None and len(Dst[i]) > 0:
        while len(Dst[i]):
            c = Dst[i].pop()
            srch(c,l+1, Lng, Dst)

def main():
    num = int(input())
    Dst = [None for i in range(num + 1)]
    Lng = [999] * (num + 1)
    
    for n in range(1, num+1):
        a = list(map(int,input().split()))
        u = a[0]
        if a[1] > 0:
            Dst[u] = a[2:]

    srch(1, 0, Lng, Dst)

    for n in range(1, num+1):
        l = Lng[n]
        if l > num:
            l = -1
        print("{} {}".format(n,l))

if __name__ == '__main__':
    main()

Python: if __name__ == '__main__':
    n = int(input())
    graph = [[0 for i in range(n)] for _ in range(n)]
    frist = [0 for _ in range(n)]
    end = [0 for _ in range(n)]
    visit = [False for _ in range(n)]
    # get the group
    for i in range(n):
        datas = [int(num) for num in input().split(' ')]
        if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

    queue = [0]
    visit = [False for _ in range(n)]
    visit[0] = True
    depths = [0 for _ in range(n)]
    while len(queue) != 0:
        point = queue.pop(0)
        for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
        if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

    for i in range(n):
        print('%d %d' % (i + 1, depths[i]))

Python: import collections
N = int(input())
P = [[0 for _ in range(N)] for _ in range(N)] 
for i in range(N):
    u,k,*varray = map(int,input().split()) # u,k は数, varray は配列
    for v in varray:
        P[u-1][v-1] = 1    

D = [-1 for _ in range(N)]
D[0] = 0 # 始点への距離は 0, 他の距離は-1
Q = collections.deque()
Q.append(0)
while len(Q) > 0:
    c = Q.popleft()
    for i in range(N):
        if P[c][i]==1 and D[i]==-1:
            D[i] = D[c]+1
            Q.append(i)
for v in range(N):
    print(v+1, D[v])

Python: from collections import deque

n = int(input())
G = []
checked = [False for i in range(n + 1)]
d = [-1 for i in range(n + 1)]
G.append([])
for i in range(n):
    u, k, *v = map(int, input().split())
    G.append(v)

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

while q:
    current = q.pop()
    for adj in G[current]:
        if not checked[adj]:
            q.append(adj)
            checked[adj] = True
            d[adj] = d[current] + 1

for id in range(1, n + 1):
    print("{0} {1}".format(id, d[id]))

Python: n = int(input())
G = [[] for _ in range(n)]
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
NIL = 100
Q = [[1, 0]]
F = []
D = [NIL for _ in range(n)]
while Q != []:
    u, d = Q.pop(0)
    if d < D[u-1]:
        D[u-1] = d
    if u not in F:
        F.append(u)
    for v in G[u-1]:
        if v not in F:
            Q.append([v, d+1])
'''
for ni in range(1, n):
    Q = [[ni+1, 0]]
    F = []
    while Q != []:
        u, d = Q.pop(0)
        if u == 1 and d < D[ni]:
            D[ni] = d
        if u not in F:
            F.append(u)
        for v in G[u-1]:
            if v not in F:
                Q.append([v, d+1])
'''
for ni in range(n):
    if D[ni] == NIL:
        print(ni+1, -1)
    else:
        print(ni+1, D[ni])

Python: from collections import deque

n = int(input())

g = [[] for _ in range(n)]
for _ in range(n):
    u, k, *V = list(map(int, input().split()))
    V.sort()
    for v in V:
        g[u-1].append(v-1)
        # g[v-1].append(u-1)
d = [-1] * n
d[0] = 0

que = deque([])
que.append(0)
while que:
    v = que.popleft()
    for i in g[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i)

for i in range(n):
    print(f'{i+1} {d[i]}')

Python: # https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C

from collections import deque


class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth


if __name__ == "__main__":
    n = int(input())
    d = {}
    for _ in range(n):
        u, k, *li = list(map(int, input().split()))
        d[u] = li

    queue = deque()

    result = {}
    for i in range(n):
        result[i+1] = -1

    depth = 0
    queue = deque()
    queue.appendleft(Data(index=1, depth=0))
    while queue:
        data = queue.pop()
        index = data.index
        depth = data.depth
        if result[index] != -1:
            continue
        result[index] = depth

        for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

    for k, v in result.items():
        print(k, v)