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 <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = (1<<21);
int n;
vv(int, M, MAX, MAX, 0);
vi d(MAX, INF);
inline void bfs() {
	d[1] = 0;
	deque<int> dq;
	dq.eb(1);
	int u;
	while(!dq.empty()) {
		u = dq.front(); dq.pop_front();
		rep(v, 1, n + 1) {
			if(M[u][v] == 0 || d[v] != INF) continue;
			d[v] = d[u] + 1;
			dq.eb(v);
		}
	}
	rep(i, 1, n + 1) {
		cout << i << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
	}
}
int main() {
	cin >> n;
	int u, k, v;
	rep(i, 1, n + 1) {
		cin >> u >> k;
		rep(j, 1, k + 1) {
			cin >> v;
			M[u][v] = 1;
		}
	}
	bfs();
	rep(i, 1, n + 1) {
		rep(j, 1, n + 1) {
			cout << M[i][j] << (j == n ? "\n" : " ");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
queue<int> q;
vector<vector<int> > v(101);
int dp[101];
int n, t = 0;
int main() {
	cin >> n;
	for (int i = 0; i < n;i++) {
		int u, k; cin >> u >> k;
		for (int j = 0; j < k;j++) {
			int x; cin >> x;
			v[u].push_back(x);
		}
	}
	memset(dp, -1, sizeof(dp));
	q.push(1); dp[1] = 0;
	while (!q.empty()) {
		int a = q.front(); q.pop();
		for (int i = 0; i < v[a].size();i++) {
			if (dp[v[a][i]] == -1) {
				dp[v[a][i]] = dp[a] + 1;
				q.push(v[a][i]);
			}
		}
	}
	for (int i = 1; i <= n;i++) {
		cout << i << " " << dp[i] << endl;
	}
}

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

void visit(int kyori[], vector<list<int> > adj){
    int k;
    queue<int> que;
    list<int>::iterator t;
    que.push(1);
    while(!que.empty()){
        k = que.front();
        que.pop();
        for(t = adj[k].begin(); t != adj[k].end(); ++t){
            if(kyori[*t] == -1){
                kyori[*t] = kyori[k] + 1;
                que.push(*t);
            }
        }
    }

}

int main(){
    //input
    int n;
    cin >> n;
    int u, k, v;
    int i, j;
    list<int> list_sub;
    vector<list<int> > adj;
    for(i = 0; i <= n; i++){
        adj.push_back(list_sub);
    }
    for(i = 1; i <= n; i++){
        cin >> u >> k;
        for(j = 1; j <= k; j++){
            cin >> v;
            adj[i].push_back(v);
        }
    }

    //calc
    int kyori[n+1];
    for(i = 2; i <= n; i++){
        kyori[i] = -1;
    }
    kyori[1] = 0;
    visit(kyori, adj);

    //answer
    for(i = 1; i <= n; i++){
        cout << i << " " << kyori[i] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string.h>

using namespace std;

int main(int argc, char* argv[]) {
  int n;
  cin >> n;

  vector<vector<int> > V(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;
    V[u - 1].resize(k);
    for (int j = 0; j < k; ++j)
      cin >> V[u - 1][j];
  }

  int d[n];
  memset(d, -1, sizeof(int) * n);
  d[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < V[cur].size(); ++i) {
      if (d[V[cur][i] - 1] == -1) {
        q.push(V[cur][i] - 1);
        d[V[cur][i] - 1] = d[cur] + 1;
      }
    }
  }

  for (int i = 0; i < n; ++i)
    cout << i + 1 << " " << d[i] << endl;

  return 0;
}