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
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int N = 100;
int d[N];
int n,M[N][N];

void dfs() {
	queue<int> q;
	q.push(0);
	int i;
	rep(i, n) d[i] = INF;
	d[0] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		int v;
		rep(v, n) {
			if (M[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	rep(u, n) {
		cout << u + 1 << ' ' << ((d[u] == INF) ? (-1) : d[u]) << '\n';
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int i;
	rep(i, n) {
		int u, k, v;
		cin >> u >> k;
		u--;
		int j;
		rep(j, k) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	dfs();
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

struct VRTX {
	int v[101] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[101];

void breadthFirstSearch(int x, int dist);

int main() {
	int i, j, id, dim;
	
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> dim;
		for (j = 0; j < dim; j++) {
			cin >> u[id].v[j];
		}
	}

	breadthFirstSearch(1, 0);

	for (i = 1; i <= n; i++) {
		cout << i << " " << u[i].dist << endl;
	}

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist <= dist)return;
	else u[x].dist = dist;
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}

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

int M[200][200];
string color[200];
queue<int> q;
int d[200];
int n;
const int INF = 1001001001;

void bfs(int s){
    rep(i, n){
        color[i] = "WHITE";
        d[i] = -1;
    }

    color[s] = "GRAY";
    d[s] = 0;
    q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for (int i = 0; i <= n; i++){
            if(M[u][i] && color[i] == "WHITE"){
                color[i] = "GRAY";
                d[i] = d[u] + 1;
                q.push(i);
            }
        }
        color[u] = "BLACK";
    }
}

int main() {
    cin >> n;
    rep(i, n){
        int a, b; cin >> a >> b;
        rep(j, b){
            int x; cin >> x;
            x--;
            M[i][x] = 1;
        }
    }
    bfs(0);
    rep(i, n){
        cout << i+1 << " " << d[i] << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>

using namespace std;
using Graph=vector<vector<int>>;

void breadth_firstsearch(Graph &graph,int vertex,queue<int> qdata,vector<int> &distance){
  distance.at(vertex)=0;
  qdata.push(vertex);
  while(!qdata.empty()){
    int ver=qdata.front();
    qdata.pop();
    for(int v : graph.at(ver)){
      if(distance.at(v)!=-1) continue;
      distance.at(v)=distance.at(ver)+1;
      qdata.push(v);
    }
  }
}


int main(void) {
	int size, vnum, adjnum, temp;
	cin >> size;
	Graph graph(size);
	queue<int> qdata;
	vector<int> distance(size,-1);

	for (int i = 0; i < size; i++) {
		cin >> vnum >> adjnum;
		for (int j = 0; j < adjnum; j++) {
			cin >> temp;
			graph.at(vnum - 1).push_back(temp - 1);
		}
	}
  breadth_firstsearch(graph,0,qdata,distance);

	for (int i = 0; i < size; i++) {
		cout << i + 1 << " " <<distance.at(i)<< endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0;i<(n);i++)
const int INF = 1001001001;
int n, u, k;
int D[100];
vector<int> edges[100];
void bfs(int src) {
	
	
	queue<int> Q;	
	Q.push(src);
	D[src] = 0;//D[*]が始点から点*への距離
	while (!Q.empty()) {
		int cur = Q.front();//curは今取り扱ってる点
		Q.pop();
		
		for (int dst = 0; dst < n; dst++) {//dstはcurから移動可能な点
			vector<int>::iterator result = find(edges[cur].begin(), edges[cur].end(), dst);
			if (result != edges[cur].end() && D[dst] == INF) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
}

int main() {
	cin >> n; //n頂点
	

	for (int i = 0; i < n; i++)//iは頂点
	{
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			int l;
			cin >> l;
			edges[u - 1].push_back(l-1);
		}//入力終わり
		D[i] = INF;
	}
		bfs(0);
	for (int i = 0; i < n; i++) {
		if (D[i] != INF)cout << i + 1 << " " << D[i] << endl;
		else cout << i + 1 << " " << -1 << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
using namespace std;

static const int N = 100;
static const int INF = (1<<21);

int n,M[N][N];
int d[N];

void bfs(int s) {
    queue<int>q;
    q.push(s);
    for(int i = 0; i< n;i++) d[i] = INF;
    d[s] = 0;
    int u;
    while(!q.empty()) {
        u = q.front();q.pop();
        for(int v = 0;v < n;v++) {
            if(M[u][v] == 0) continue;
            if(d[v] != INF) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }
    for(int i = 0;i < n; i++) {
        cout << i+1 << " " << ( (d[i] == INF) ? (-1):d[i]) << endl;
    }}

    int main() {
        int u,k,v;
        rep(i,n) {
            rep(j,n) {
                M[i][j] = 0;
            }
        }
        cin >> n;
        for(int i = 0;i < n;i++) {
            cin >> u >> k;
            u--;
            for(int j = 0;j<k;j++) {
                cin >> v;
                v--;
                M[u][v] = 1;
            }
        }
        bfs(0);
        return 0;
    }