Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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;
}