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 <vector>
#include <queue>

using namespace std;

void bfs(int src, int n, vector<vector<int> > G);

int main() {
	int n;
	cin >> n;
	vector<vector<int> > G(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v_j;
			cin >> v_j;
			G[u - 1][v_j - 1] = 1;
		}
	}
	bfs(0, n, G);
}

void bfs(int src, int n, vector<vector<int> > G) {
	queue<int> Q;
	Q.push(src);
	vector<int> D(n, 0);
	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int dst = 0; dst < n; dst++)
			if (G[cur][dst] == 1 && D[dst] == 0){
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
	}
	for (int i = 0; i < n; i++)
		cout << i + 1 << " " << D[i] << endl;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	int n;
	cin>>n;
	vector<int> k(n),d(n);
	vector<vector<int>> v(n);
	rep(i,n){
		int w;
		cin>>w;
		cin>>k[i];
		v[i].resize(k[i]);
		rep(j,k[i]){
			cin>>v[i][j];
			v[i][j]--;
		}
		d[i]=-1;
	}

	queue<int> q;
	q.push(0);
	d[0]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		rep(i,k[x]){
			if(d[v[x][i]]==-1){
				d[v[x][i]]=d[x]+1;
				q.push(v[x][i]);
			}
		}
	}
	rep(i,n){
		cout<<i+1<<" "<<d[i]<<endl;
	}
}

C++: #include<iostream>
#include<queue>
const int N = 100;
const int INFTY = (1<<21);
using namespace std;
int n,M[N][N],d[N];
void bfs(int u){
    queue<int> q;
    q.push(u);
    for(int i=0;i<n;i++) d[i] = INFTY;
    d[u] = 0;
    while(!q.empty()){
        int m = q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(M[m][i]&&d[i]==INFTY){
                d[i] = d[m] + 1;
                q.push(i);
            }
        }
    }
    for(int i=0;i<n;i++)
        cout<<i+1<<" "<<(d[i]==INFTY?-1:d[i])<<endl;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int u,k,v;
		scanf("%d%d",&u,&k);
		while(k--){
			scanf("%d",&v);
			M[u-1][v-1] = 1;
		}
	}
	bfs(0);
	return 0;
}

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

const int INF = 1001001001;

void bfs(vector< vector<int> >& edges, vector<int>& d, int node)
{
    d[node] = 0;
    queue<int> Q;
    Q.push(node);
    while(!Q.empty())
    {
        node = Q.front();
        Q.pop();
        for(int e : edges[node])
        {
            if(d[e] == INF)
            {
                d[e] = d[node] + 1;
                Q.push(e);
            }
        }
    }
}

int main()
{
    int n, u, k, v;
    cin >> n;
    vector<int> d(n);
    vector< vector<int> > edges(n);
    for(int i=0;i<n;++i)
    {
        d[i] = INF;
        cin >> u >> k;
        --u;
        for(int j=0;j<k;++j)
        {
            cin >> v;
            edges[u].push_back(--v);
        }
    }
    bfs(edges, d, 0);
    for(int i=0;i<n;++i)
    {
        cout << i+1 << " ";
        if(d[i] == INF) cout << -1;
        else cout << d[i];
        cout << endl;
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
void bfs(int);
int n,M[N][N],d[N],u,i,j;
string color[N];
int main(){
  int inu,k,v;
  cin >> n;
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      M[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    cin >> inu >> k;
    for(j=0;j<k;j++){
      cin >> v;
      M[inu][v]=M[v][inu]=1;
    }
  }
  bfs(1);
  for(i=1;i<=n;i++) cout << i << ' ' << d[i] << endl;
  return 0;
}
void bfs(int s){
  queue<int> q;
  for(i=1;i<=n;i++){
    color[i]="WHITE";
    d[i]=1000000000;
  }
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=true){
    u=q.front();
    q.pop();
    for(i=1;i<=n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
}

C++: #include <bits/stdc++.h>
#include <cmath>

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
const double INF = 1e10;
//const ll INF = LONG_LONG_MAX / 10;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

typedef pair<double, double> P;

void print_line(vector<int> &line) {
    if (line.size() == 0ll) {
        cout << endl;
        return;
    }
    for (ll i = 0; i < line.size(); i++) {
        cout << line[i];
        if (i == line.size() - 1) cout << endl;
        else cout << ' ';
    }
}


typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;
const int mod = 1000000007;

struct Q {
    int to, depth;
};

void bfs(int from, vector<vector<int>> &tos, set<int> &used, vector<int> &ans) {
    queue<Q> q;
    q.push({from, 0});

    while (!q.empty()) {
        Q top = q.front();
        q.pop();
        if (!contain(used, top.to)) {
            continue;
        }

        used.erase(top.to);
        ans[top.to] = top.depth;

        for (int to : tos[top.to]) {
            q.push({to, top.depth + 1});
        }
    }

}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> tos(n);
    rep(i, n) {
        int j, k;
        cin >> j >> k;
        j--;
        rep(l, k) {
            int m;
            cin >> m;
            m--;
            tos[j].push_back(m);
        }
    }

    vector<int> ans(n, -1);
    rep(i, n) sort(tos[i].begin(), tos[i].end());
    set<int> used;
    rep(i, n) used.insert(i);
    bfs(0, tos, used, ans);

    rep(i, n) cout << (i + 1) << ' ' << ans[i] << endl;
}