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;

static const int N=100;
static const int INFTY=(1<<21);

int n,m[N][N];
int d[N];

void dfs(int a){
  queue<int> q;
  q.push(a);
  for(int i=0;i<n;i++) d[i]=INFTY;
  d[a]=0;
  int b;
  while(!q.empty()){
    b=q.front();
    q.pop();
    for(int j=0;j<n;j++){
      if(m[b][j]==0) continue;
      if(d[j]!=INFTY) continue;
      d[j]=d[b]+1;
      q.push(j);
    }
  }
  for(int i=0;i<n;i++) cout << i+1 << " " << ((d[i]==INFTY) ? (-1) : d[i]) << endl;
}

int main(){
  int u,k,v;

  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) m[i][j]=0;
  }

  for(int i=0;i<n;i++){
    cin >> u >> k;
    u--;
    for(int j=0;j<k;j++){
      cin >> v;
      v--;
      m[u][v]=1;
    }
  }
  dfs(0);
  return 0;
}

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

int graph[N][N];
int dis[N];
int check[N];
int n;
queue<int> q;

void init(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            graph[i][j] = 0;  //图初始化
        }
        check[i]=0;           //标记数组初始化
        dis[i] = -1;
    }
    int v,count,x;
    for(int i=0;i<n;i++){
        cin>>v;
        cin>>count;
        for(int i=0;i<count;i++){
            cin>>x;
            graph[v-1][x-1]=1;
        }
    }
    q.push(1);
    check[0]=1;
    dis[0]= 0;
}

int BFS(int v){
    for(int i=0;i<n;i++){
        if(graph[v-1][i]>0&&check[i]==0){
            q.push(i+1);
            check[i]=1;
            dis[i] = dis[v-1]+1;
        }
    }
    q.pop();
}

int main(){
    init();
    while(q.size()>0){
        BFS(q.front());
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<dis[i]<<endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i,n)   for (int i = 0; i < (n); ++i)
#define sort(a)    sort((a).begin(), (a).end());
#define uniq(a)    SORT(a);(a).erase(unique((a).begin(), (a).end()), (a).end());
#define find(a,x)  find((a).begin(), (a).end(), (x)) != (a).end()
#define reverse(a) reverse((a).begin(), (a).end());
#define out(d) cout << (d);
#define outl(d) std::cout<<(d)<<"\n";
#define Yes() printf("Yes\n");
#define No() printf("No\n");
#define YES() printf("YES\n");
#define NO() printf("NO\n");
#define ceil(x, y) ((x + y - 1) / (y))

using namespace std;
using ll = long long;
using P = pair<ll,ll>;

const ll MOD = 1000000007; // 10^9 + 7

const int MAX_N = 100;
int d[MAX_N + 1];
int N;
vector<vector<int>> g(MAX_N + 1);

void bfs() {
  queue<P> q;
  q.push(make_pair(1, 0));

  while (!q.empty()) {
    P p = q.front(); q.pop();
    int node = p.first;
    int dist = p.second;

    if (d[node] >= 0) continue;

    d[node] = dist;
    for (int i = 0; i < g[node].size(); i++) {
      q.push(make_pair(g[node][i], dist + 1));
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));

  cin >> N;
  for (int i = 0; i < N; i++) {
    int u; cin >> u;
    int k; cin >> k;
    for (int ki = 0; ki < k; ki++) {
      int tmp; cin >> tmp;
      g[u].push_back(tmp);
    }
    sort(g[u])
  }
  memset(d, -1, sizeof(d));

  bfs();

  for (int i = 1; i <= N; i++) {
    cout << i << " " << d[i] << endl;
  }
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];
void bfs(int s) {
    queue<int> q;
    q.push(s);
    for ( int i = 0; i < n; i++ ) d[i] = INFTY;
    
    d[s] = 0;
    int u;
    while ( !q.empty() ) {
        u = q.front();
        q.pop();
        for ( int v = 0; v < n; v++ ) {
            if ( M[u][v] == 0 ) continue;
            if ( d[v] != INFTY ) continue;
            d[v] = d[u] + 1;
            q.push(v);
            }
        
    }
    for ( int i =0; i<n; i++){
         cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
         }
    
}
         int main() {
             int u, k, v;
             cin >> n;
             for ( int i =0; i<n; i++){
                 for ( int j=0; j<n; j++)  M[i][j] = 0;
                       }
             
             for ( int i =0; i<n; i++){
                            cin >> u >> k;
                 u--;
                 for ( int j =0; j<k; j++){
                                 cin >> v;
                     v--;
                     M[u][v] =1;
                                 }
                 
             }
           bfs(0);
       return 0;
             
         }

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

using namespace std;

int main() {
  int n; cin >> n;

  vector<vector<int>> G(n, vector<int>());

  for (int i = 0; i < n; i++) {
    int u, k; cin >> u >> k; u--;
    for (int j = 0; j < k; j++) {
      int v; cin >> v; v--;
      G[u].push_back(v);
    }
  }

  vector<int> dist(n, 1000);
  dist[0] = 0;

  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      for (auto k: G[j]) {
        dist[k] = min(dist[k], dist[j] + 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i + 1 << ' ';
    if (dist[i] == 1000) {
      cout << -1 << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) d[i] = INFTY;
  d[s] = 0;
  int u;
  while( !q.empty() ) {
    u = q.front(); q.pop();
    for (int v = 0; v < n; v++) {
      if(M[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) M[i][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}