Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

int n;
vector<int> G[MAX];
int d[MAX];

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		d[i] = -1;
	}
	for (int i = 0, u, k; i < n; i++) {
		cin >> u >> k;
		for (int j = 0, v; j < k; j++) {
			cin >> v;
			G[u - 1].push_back(v - 1);
		}
	}
	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		for (int &v : G[q.front()]) {
			if (d[v] == -1) {
				q.push(v);
				d[v] = d[q.front()] + 1;
			}
		}
		q.pop();
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << ' ' << d[i] << endl;
	}
	return 0;
}

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

using namespace std;
using ll = long long;

int n;
vector<vector<int>> hen;
vector<bool> seen;
vector<int> dist;

void bfs()
{
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    seen[0] = true;
    while(que.size())
    {
        auto cur = que.front();
        que.pop();
        for(auto i : hen[cur]){
            if(seen[i])continue;
            seen[i] = true;
            dist[i] = dist[cur] + 1;
            que.push(i);
        }
    }
}




int main()
{
    cin >> n;
    hen.resize(n);
    dist.resize(n, -1);
    seen.resize(n);
    for(int i = 0; i < n; i++){
        int u, k; cin >> u >> k; u--;
        for(int j = 0; j < k; j++){
            int tmp; cin >> tmp; tmp--;
            hen[u].push_back(tmp);
        }
    }
    bfs();

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


}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[10000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	  T[num].A[i]=0;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

  for(int i=1;i<=n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }


  return 0;
}

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

struct node{
    int d, flag;
    node(){
        d = 0;
        flag = 0;
    }
};

int main(void){
    int n;
    int ind, num, a;
    int time, ndi;

    std::cin >> n;
    std::vector<node> nd(n);
    std::vector<std::vector<int>> b(n, std::vector<int>(n));
    std::queue<int> st;

    for(int i = 0; i < n; i++){
        std::cin >> ind >> num;
        ind--;
        for(int i = 0; i < num; i++){
            std::cin >> a;
            a--;
            b[ind][a] = 1;
        }
    }

    time = 0;
    int x = 0;
    //for(int x = 0; x < n; x++){
        if(nd[x].flag == 0){
            ndi = x;
            time++;
            nd[x].flag = 1;
        
            st.push(x);
        }
    
        

        while(st.empty() != 1){
            //std::cout << ndi+1 << " " << time << std::endl;
            //time++;
            int tmp = -1;
            int tmp2;
            for(int i = 0; i < n; i++){
                if(b[ndi][i] == 1 && nd[i].flag == 0){
                    tmp = i;
                    tmp2 = x;
                    break;
                }
            }
            if(tmp == -1){
                //std::cout << ndi+1 << " " << time << std::endl;

                nd[ndi].flag = 1;
                st.pop();
                if(st.empty() != 1){
                    ndi = st.front();
                }

                continue;
            }

            nd[tmp].flag = 1;
            nd[tmp].d = nd[ndi].d + 1;
            st.push(tmp);
            
        }
    //}
    for(int i = 0; i < n; i++){
        if(nd[i].flag == 0 && i != 0){
            nd[i].d = -1;
        }
    }
    for(int i = 0; i < n; i++){
        std::cout << i+1 << " " << nd[i].d << std::endl;
    }

    return 0;
}

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

using namespace std;

int color[100];
int d[100];
int f[100];
int G[100][100];
int nt[100];
int n;

int WHITE = 1;
int GRAY = 2;
int BLACK = 3;

int _time = 0;

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (G[u][v]) return v;
  }

  return -1;
}

void dfs(int r) {
  for (int i = 0; i < n; i++) nt[i] = 0;

  queue<int> S;

  S.push(r);
  color[r] = GRAY;

  while(!S.empty()){
    int u = S.front();

    while(1) {
      int v = next(u);

      if(v != -1) {
        if(color[v] == WHITE) {
          color[v] = GRAY;

          S.push(v);

          d[v] = d[u] + 1;
        }
      } else {
        break;
      }
    }

    S.pop();
  }
}

void dfs_init() {
  for(int i = 0; i < n; i++) {
    color[i] = WHITE;
  }

  dfs(0);

  for(int i = 0; i < n; i++) {
    if(color[i] == WHITE) {
      d[i] = -1;
    }
  }
}

int main() {
  int m, k, l;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    cin >> m;

    for (int j = 0; j < m; j++) {
      int l;
      cin >> l;

      G[k-1][l-1] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    dfs_init();
  }

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

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;

vector<int>E[100];
int d[100];
int main(){
	int n;cin>>n;
	rep(i,n){
		int u,k;cin>>u>>k;u--;
		rep(i,k){
			int v;cin>>v;v--;
			E[u].push_back(v);
		}
	}
	memset(d,0x3f,sizeof(d));
	queue<P>que;
	d[0]=0;que.push(P(0,0));
	while(!que.empty()){
		P p=que.front();que.pop();
		for(int u:E[p.second]){
			if(d[u]>p.first+1){
				d[u]=p.first+1;
				que.push(P(d[u],u));
			}
		}
	}
	rep(i,n){
		if(d[i]==INF)cout<<i+1<<" -1"<<endl;
		else cout<<i+1<<' '<<d[i]<<endl;
	}
}