Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

Python: from collections import deque

N = int(input())

nodes = [tuple(map(int, input().split(' '))) for _ in range(N)]

distances = [-1] * (N + 1)

queue = deque()
queue.append((1, 0))

while len(queue) > 0:
    node, distance = queue.popleft()
    if distances[node] != -1:
        continue
    distances[node] = distance

    neighbors = nodes[node - 1][2:]
    for neighbor in neighbors:
        queue.append((neighbor, distance + 1))

for node in range(1, N + 1):
    print(node, distances[node])

Python: #coding: utf-8
inf = 100000000
n = int(input())

List = [0] + [list(map(int,input().split())) for i in range(n)]
color = [0] + ["white" for  i in range(n)]
d = [inf for i in range(n+1)]

s = 1
Q = [s]
d[s] = 0
while Q != []:
    q = Q.pop()
    if color[q] == "white":
        k = List[q][1]
        if k != 0:
            Q += List[q][2:2+k]
            for i in List[q][2:2+k]:
                d[i] = min(d[q] + 1, d[i])
    
    color[q] = "black"


for i, dis in enumerate(d[1:], 1):
    if dis == inf:
        dis = -1
    print(i, dis)

Python: # coding: utf-8
import queue

q = queue.Queue()
A = [[]*101 for _ in range(101)]
d = [-1]*101
visited = [False]*101

n = int(input().rstrip())
for i in range(n):
    line = [int(j) for j in input().rstrip().split()]
    A[i+1].extend(line[2:])

d[1] = 0

q.put(1)
while not q.empty():
    v = q.get()
    if visited[v] == True:
        continue
    visited[v] = True
    for i in A[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
        else:
            d[i] = min(d[i],d[v] + 1)
        q.put(i)
        
for i in range(n):
    print(str(i+1)+" "+str(d[i+1]))

Python: import queue

n = int(input())

Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)]


# for i in range(n):
#     li = list(map(int, input().split()))
#     a = li[0]
#     a-=1
#     b = li[2:]
#     Graph[a].append(b)


dist = [-1]*n
q = queue.Queue()

dist[0] = 0
q.put(0)

while not q.empty():
    v = q.get()

    for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv)


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

Python: INF = 10**18 + 7


def main():
    from collections import deque
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        u, k, *A = (int(i)-1 for i in input().split())
        G[u] = A

    def bfs(s=0):
        que = deque([])
        dist = [INF]*N
        dist[s] = 0
        que.append(s)
        while que:
            pos = que.popleft()
            for v in G[pos]:
                if dist[v] != INF:
                    continue
                dist[v] = dist[pos] + 1
                que.append(v)
        return dist

    dist = bfs()
    for i, d in enumerate(dist, start=1):
        print(i, d if d != INF else -1)


if __name__ == '__main__':
    main()

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using Graph = vector<vector<int>>;

int main(){
    int n; cin>>n;
    Graph G(n);
    rep(i,n){
        int u,k; cin>>u>>k;
        rep(j,k){
            int v; cin>>v;
            G[u-1].push_back(v-1);
        }
    }

    vector<int> dist(n,-1);
    queue<int> que;

    dist[0]=0;
    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for(auto nv : G[v]){
            if(dist[nv]!=-1)continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i,n){
        cout<<i+1<<" "<<dist[i]<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <queue>
using namespace std;
struct bfs{
    int n;
    int l;
};
int main(){
    int n,a,m,temp,ans[101];
    bool graph[101][101];
    bfs t,b;
    queue<bfs> q;
    scanf("%d",&n);
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++){
            graph[i][j]=false;
        }
        ans[i]=-1;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&m);
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            graph[a][temp]=true;
        }
    }
    t.n=1;
    t.l=0;
    q.push(t);
    while(!q.empty()){
        t=q.front();
        q.pop();
        if(ans[t.n]==-1)ans[t.n]=t.l;
        else continue;
        t.l++;
        b=t;
        for(int i=1;i<=n;i++){
            if(graph[t.n][i]){
                b.n=i;
                q.push(b);
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,ans[i]);
    }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
vector<int> v[101];
int d[101];
queue<int> q;
bool flag[101];
int main(){
	int n, u, k; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j){
			int tmp; scanf("%d", &tmp);
			v[u].push_back(tmp);
		}
	}
	memset(d, -1, sizeof(d));
	d[1] = 0;
	q.push(1); flag[1] = true;
	while (!q.empty()){
		int m = q.front(); q.pop();
		for (int i = 0; i < v[m].size(); ++i){
			int tmp = v[m][i];
			if (!flag[tmp]){
				flag[tmp] = true;
				d[tmp]=d[m]+1;
				q.push(tmp);
			}
		}
	}
	for (int i = 1; i <= n; ++i){
		cout << i << ' ' << d[i] << '\n';
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const int MAX = 105;

vector<int> to[MAX];
bool seen[MAX];
int d[MAX];

void bfs(int u) {
  queue<int> q;
  seen[u] = true;
  d[u] = 0;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v: to[u]) {
      if (seen[v]) continue;
      d[v] = d[u] + 1;
      seen[v] = true;
      q.push(v);
    }
  }
}

int main() {
  int n, u, k, v;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      to[u-1].push_back(v-1);
    }
  }

  rep(i, n) {
    seen[i] = false;
    d[i] = INF;
  }

  bfs(0);

  rep(i, n) {
    cout << i+1 << " ";
    if (seen[i]) cout << d[i] << endl;
    else cout << -1 << endl;
  }



  
  return 0;
}