Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

C++: #include <iostream>


using namespace std;

bool adjmatrix[101 * 101];
int costs[101];



void BFS(int vertex, int cur_cost, int N) {
  if (costs[vertex] < cur_cost) return;
  costs[vertex] = cur_cost;
  for (int i =1; i<= N; i++) {
    if(adjmatrix[vertex*101 + i]) {
      BFS(i, cur_cost + 1, N);
    }
  }
}
  

int main() {
  for (int i = 0; i < 101; i++) {
    costs[i] = 101;
  }
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    int vertex, degree;
    cin >> vertex >> degree;

    for (int j = 0; j < degree; j++) {
      int outvertex;
      cin >> outvertex;
      adjmatrix[vertex * 101 + outvertex] = true;
    }
  }


  /*
  //function to print adjmatrix
  for (int i = 0; i <= N; i++) {
    for (int j= 0; j <= N; j++) {
      if (adjmatrix[i * 101 + j]) {
	cout << "1 ";
      } else {
	cout << "0 ";
      }
    }
    cout << endl;
    }*/

  BFS(1, 0, N);

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

 

}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<queue>
#include<string>
using namespace std;

vector< vector<int> > L(105);
vector<int> d(105,-1);
vector<bool> checked(105,0);

void BFS(int s){
	queue<int> Q;
	int i;
	int v;
	
	Q.push(s);
	d[s]=0;
	while(!Q.empty()){
		v=Q.front();
		Q.pop();
		
		for (i=0;i<L[v].size();i++){
			if (d[L[v][i]]==-1){
				Q.push(L[v][i]);
				d[L[v][i]]=d[v]+1;
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,i,j;
	cin >> n;
	
	int id, deg, v;
	for (i=0;i<n;i++){
		cin >> id >> deg;
		for (j=0;j<deg;j++){
			cin >> v;
			L[id-1].push_back(v-1);
		}
	}
	
	BFS(0);
	
	for (i=0;i<n;i++){
		cout << i+1 << " " << d[i] << endl;
	}
	return 0;
	
}

C++: #include <iostream>
#include <queue>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

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

void bfs(int s){
  for(int i = 0 ; i < n ; i++){
    color[i] = WHITE;
    d[i] = 1000000000;
  }
  color[s] = GRAY;
  d[s] = 0;
  queue<int> Q;
  Q.push(s);

  while(!Q.empty()){
    int u = Q.front();
    Q.pop();
    for(int v = 0; v < n ; v++){
      if(M[u][v] && color[v] == WHITE){
        color[v] = GRAY;
        d[v] = d[u] + 1;
        Q.push(v);
      }
    }
    color[u] = BLACK;
  }
  for(int i = 0 ; i < n ; i++){
    cout << i+1 << " " << ((d[i] == 1000000000) ? (-1) :d[i]) << "\n";
  }
}

int main(){
  int u , k , A;
  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 >> A;
      M[u][A-1] = 1;
    }
  }

  bfs(0);
  return 0;
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>


int n;
int a[105][105];
std::queue<int>S;
int dis[105];
int color[105];

void bfs(int k){
    int i;
    for(i=1;i<=n;i++){
        if(i!=k&& a[k][i]==1 &&color[i]==0){
            S.push(i);
            dis[i]=dis[k]+1;
            color[i]=1;
        }
    }
}


int main(){
    scanf("%d",&n);
    int i,j;
    for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;
    for(i=1;i<=n;i++){
        int p,k,q;
        scanf("%d %d",&p ,&k);
        for(j=1;j<=k;j++){
            scanf("%d",&q);
            a[p][q]=1;
        }
    }
    for(i=1;i<=n;i++)dis[i]=-1;
    dis[1]=0;
    for(i=1;i<=n;i++)color[i]=0;//0???????¨????
    S.push(1);
    color[1]=1;
    while(!S.empty()){
        int front=S.front();
        S.pop();
        bfs(front);
    }
    for(i=1;i<=n;i++){
        printf("%d %d\n",i,dis[i]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

using Graph = vector<vector<int> >;

vector<int> dist;
queue<int> que;

void bfs(const Graph &G){
  while(!que.empty()){
    int v = que.front();
    que.pop();

    for(int nv : G[v]){
      if(dist[nv] != -1) continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
}

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

  Graph G(n+1);
  rep(i, n){
    int u, k; cin >> u >> k;
    rep(j, k){
      int v; cin >> v;
      G[u].push_back(v);
    }
  } 

  dist.assign(n+1, -1);
  dist[1] = 0;
  que.push(1);
  bfs(G);

  REP(i, 1, n){
    cout << i << " " << dist[i] << endl;
  }

  return 0;
}

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

const int INF = -1;

int G[110][110] = {};
int d[110] = {};

void bfs(int s, int n)
{
  int v;
  queue<int> que;

  for(int i = 1;i <= n;i++) d[i] = INF; //init

  que.push(s);
  d[s] = 0;
  
  while(que.size()){
    v = que.front();
    que.pop();

    for(int i = 1;i <= n;i++){
      if(G[v][i] != 1) continue;
      if(d[i] != INF) continue;

      que.push(i);
      d[i] = d[v]+1;
    }
  }
}

int main()
{
  int n;
  int i,j;
  int u,k,v;

  scanf("%d", &n);

  for(i = 0;i < n;i++){
    scanf("%d%d", &u, &k);

    for(j = 0;j < k;j++){
      scanf("%d", &v);
      G[u][v] = 1;
    }
  }

  /*for(i = 1;i <= n;i++)
    if(d[i] == 0) bfs(i, n);
  */
  bfs(1, n);
  
  for(i = 1;i <= n;i++)
    printf("%d %d\n", i, d[i]);

  return 0;
}