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

using namespace std;
int main(){
  int u, i, id, k, j;
  cin >> u;
  unordered_map<int, vector<int> > mymap;
  queue<vector<int> > myqueue;
  for (i = 0; i < u; ++i){
    cin >> id >> k;
    vector<int> points;
    for (j = 0; j < k; ++j){
      int a;
      cin >> a;
      points.push_back(a);
    }
    mymap[id] = points;
    if(id == 1){
      myqueue.push(points);
    }
  }
  
  bool visited[u+1];
  for (j = 1; j <= u; ++j){
    visited[j] = false;
  }
  visited[1] = true;

  int dist[u+1];
  for (j = 1; j <= u; ++j){
    dist[u] = -1;
  }
  dist[1] = 0;
  int d = 1;

  while(!myqueue.empty()){
    vector<int> combined;
    for (i = 0; i < myqueue.front().size(); ++i){
      if(!visited[myqueue.front()[i]]){
        dist[myqueue.front()[i]] = d;
        visited[myqueue.front()[i]] = true;
        if(combined.empty()){
          combined = mymap[myqueue.front()[i]];
        }else{
          combined.insert(combined.end(), mymap[myqueue.front()[i]].begin(), mymap[myqueue.front()[i]].end());
        }
      }
    }
    if(!combined.empty()){
      myqueue.push(combined);
    }
    myqueue.pop();
    d++;
  }

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

}

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

int main()
{
    queue<int> Q;
    int n;
    int a, b, c;
    cin >> n;
    int D[100], in[100][100];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
#define MAX_N 100
int d[MAX_N];

void init(){
	rep(i,MAX_N){
		d[i] = -1;
	}
}


int main()
{
	init();
	int n;
	cin>>n;
	vector<int> *node = new vector<int>[n];
	rep(i,n){
		int num,k;
		cin>>num>>k;
		rep(j,k){
			int num;
			cin>>num;
			num--;
			node[i].push_back(num);
		}
	}
	
	d[0] = 0;
	queue<int> que;
	que.push(0);
	while(que.empty() != true){
		int currentNode = que.front();
		que.pop();
		for(auto iter = node[currentNode].begin(); iter != node[currentNode].end(); iter++){
			if( d[*iter] == -1 || d[currentNode] + 1 < d[*iter]){
				d[*iter] = d[currentNode] + 1;
				que.push(*iter);
			}
		}
	}
	rep(i,n)cout<<i+1<<" "<<d[i]<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<n;++i)
#define SET(a,c) memset(a,c,sizeof(a))

const int MAX_N = 100;
const int INF = 0x3F3F3F3F;

int n, d[MAX_N], G[MAX_N][MAX_N] = {0};

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

int main() {
  int u, k, v;
  cin >> n;
  REP(i, n) {
    cin >> u >> k;
    REP(j, k) {
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }
  bfs(0);
  return 0;
}

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

using namespace std;
static const int R =100;
static const int IF = (1<<21);

int num,Mi[R][R],i,x,y;
int D[R];

void BF(int A){
  queue<int> Q;
  Q.push(A);
  for(i = 0;i<num;i++){
    D[i] = IF;
  }
  D[A] = 0;

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

    for(y=0;y<num;y++){
      if(Mi[x][y] == 0){
	continue;
      }
      if(D[y] != IF){
	continue;
	  }
      D[y] = D[x] +1;
      Q.push(y);
    }
  }
  for(i =0; i<num; i++){
    cout << i+1 << " " <<((D[i] ==IF) ? (-1) : D[i]) << endl;
  }
}

  int main(){
    int x,z,y,i,j;

    cin >> num;

    for(i=0;i<num;i++){
      for(j=0;j<num;j++){
	Mi[i][j] = 0;
      }
    }

    for(i = 0;i<num;i++){
      cin >> x >>z;
      x--;

      for(j=0;j<z;j++){
	cin>>y;
	y--;
	Mi[x][y] =1;
      }
    }

    BF(0);

    return 0;
  }

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

int main()
{
    queue<int> Q;
    int n,src=0;
    int a, b, c;
    cin >> n;
    int D[n], in[n][n];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty)
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}