Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vector<int> g[101];
bool visited[101]{};
int n, ans[101]{};

void bfs() {
    fill(ans, ans + n + 1, -1);
    ans[1] = 0;
    queue<P> que;
    que.push({1, 0});
    visited[1] = true;
    while (!que.empty()) {
        int u, d;
        u = que.front().first;
        d = que.front().second;
        que.pop();
        REP(i, g[u].size()) {
            if (visited[g[u][i]]) continue;
            que.push({g[u][i], d + 1});
            visited[g[u][i]] = true;
            ans[g[u][i]] = d + 1;
        }
    }
}

int main() {
    cin >> n;
    REP(i, n) {
        int u, m;
        cin >> u >> m;
        REP(j, m) {
            int v;
            cin >> v;
            g[u].push_back(v);
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        cout << i << " " << ans[i] << endl;
    }
    return 0;
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}

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

const int MAX = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int N;
int M[MAX][MAX];

int d[MAX];

queue<int> Q;

void bfs() {
    for (int i = 0; i < N; i++) {
        d[i] = -1;
    }
    
    Q.push(0);
    d[0] = 0;

    while (!Q.empty()) {
        int u = Q.front(); Q.pop();

        for (int i = 0; i < N; i++) {
            if (M[u][i] == 1 && d[i] == -1) {
                Q.push(i);
                d[i] = d[u] + 1;
            }
        }
    }
}

int main() {
    int u, k;
    
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%d %d", &u, &k);
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs();
    
    for (int i = 0; i < N; i++) {
        printf("%d %d\n", i + 1, d[i]);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
void func(vector<vector<int> > &G, int *depth)
{
    queue<int> q;
    int p = 1;
    q.push(p);
    depth[p] = 0;
    while (q.empty() == false)
    {
        p = q.front();
        q.pop();
        rep(i, 0, G[p].size())
        {
            if (depth[G[p][i]] == -1)
            {
                q.push(G[p][i]);
                depth[G[p][i]] = depth[p] + 1;
            }
        }
    }
}
int main()
{
    int n;
    cin >> n;
    int ans[n + 1], u, k, v;
    vector<vector<int> > G(n + 1, vector<int>(0));
    rep(i, 0, n)
    {
        ans[i + 1] = -1;
        cin >> u >> k;
        rep(j, 0, k)
        {
            cin >> v;
            G[u].push_back(v);
        }
    }
    func(G, ans);
    rep(i, 1, n + 1)
    {
        printf("%d %d\n", i, ans[i]);
    }
}

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

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main(){
  int n;cin>>n;
  vector<ll> g[n+1];
  rep(i, n){
    ll u,k;cin>>u>>k;
    rep(j, k){
      ll t;cin>>t;
      g[u].push_back(t);
    }
  }

  vector<ll>result(n+1);
  rep1(i, n)result[i]=-1;

  result[1]=0;

  queue<ll> q;
  q.push(1);
  while(!q.empty()){
    ll c = q.front();
    q.pop();
    for(auto to: g[c]){
      if(result[to]>=0)continue;

      result[to] = result[c]+1;
      q.push(to);
    }
  }

  rep1(i, n)cout<<i<<' '<<result[i]<<endl;

  return 0;
}

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

int n;
queue<int> q;
vector<int> depth;

void dfs(int start, vector<vector<int>> a) {
    depth[start] = 0;
    q.push(start);

    while (!q.empty()) {
        int current = q.front();
        q.pop();
        rep(adj, n) {
            if (a[current][adj] == 1 && depth[adj] == -1) {
                depth[adj] = depth[current] + 1;
                q.push(adj);
            }
        }
    }
}

int main() {
    scanf("%d", &n);
    depth.resize(n+1, -1);
    vector<vector<int>> a(n+1, vector<int>(n+1, 0));
    rep(i, n) {
        int u;
        scanf("%d", &u);
        int k;
        scanf("%d", &k);
        rep(j, k) {
            int adj;
            scanf("%d", &adj);
            a[u][adj] = 1;
        }
    }
    dfs(1, a);
    rep(i, n) {
        printf("%d %d\n", i, depth[i]);
    }
}