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: class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p


def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d







n = int(input())
G = {}
for i in range(n):
  entry = list(map(int, input().split(' ')))
  u = entry[0]
  k = entry[1]
  G[u] = set()
  for v in entry[2:]:
    G[u].add(v)


d = solve(G)
for u in G:
  print(u,d[u])

Python: from collections import deque
n=int(input())
d=[-1]*(n+1)
d[1]=0
v=[[] for i in range(n+1)]
for i in range(n):
  a=list(map(int,input().split()))
  v[a[0]]=a[2:]
q=deque()
q.append(1)
while len(q)>0:
  curr=q.popleft()
  for i in v[curr]:
    if d[i]==-1:
      d[i]=d[curr]+1
      q.append(i)
for i in range(1,n+1):
  print(f'{i} {d[i]}')

Python: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
from Queue import Queue
 
 
def main():
    num = int(stdin.readline())
 
    L = []
    for _ in xrange(num):
        L.append([int(s) for s in stdin.readline().split()[2:]])
 
    d = [-1] * num
    d[0] = 0
    q = Queue(100)
    q.put(0)
    while not q.empty():
        u = q.get()
        for v in L[u]:
            if d[v-1] < 0:
                d[v-1] = d[u] + 1
                q.put(v-1)
 
    for i, v in enumerate(d):
        print(i+1, v)
 
 
main()

Python: from collections import deque

n = int(input())
C = [[] for _ in range(n+1)]
for _ in range(n):
    stdin = list(map(int,input().split()))
    for j in range(stdin[1]):
        C[stdin[0]].append(stdin[2+j])

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

while q:
    node = q.popleft()
    for adj in C[node]:
        if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True

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

Python: n=int(input())

lst=[0]*n

for _ in range(n):
    work=list(map(int,input().split()))
    lst[work[0]-1]=work[2:]

dis=[-1]*n
dis[0]=0
work=lst[0][:]
work_next=[]
token=0

while work:
    token+=1
    for x in work:
        if dis[x-1]==-1: dis[x-1]=token
        for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
    work=work_next[:]
    work_next=[]

for i in range(n):
    print("{} {}".format(i+1,dis[i]))

Python: import collections
N = int(input().strip())
Alst = []
for _ in range(N):
    u,k,*varray = map(int,input().strip().split(" "))
    Alst.append(varray)
D = [-1 for _ in range(N)]
D[0] = 0
Q = collections.deque()
Q.append(0)
while len(Q) > 0:
    cur = Q.popleft()
    for dst in range(N):
            if (dst+1 in Alst[cur]) and (D[dst] == -1):
                D[dst] = D[cur] + 1
                Q.append(dst)
for v in range(N):
    print(v+1,D[v])