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)
}

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;
}