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

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

	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<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class Graph{
public:
  int v;
  int cost = inf;
  int ftime = -1;
  int ltime = -1;
  vector<int> e;
};
vector<Graph> g;
int cnt = 0;
void dfs(int pos){
  if( g[pos].ftime != -1 ) return;
  cnt++;
  g[pos].ftime = cnt;
  for(int i=0; i<g[pos].e.size(); i++){
    dfs(g[pos].e[i]);
  }
  cnt++;
  g[pos].ltime = cnt;
}
void bfs(int pos){
  for(int i=0; i<g[pos].e.size(); i++){
    // if( g[ g[pos].e[i] ].cost != inf ) continue;
    g[ g[pos].e[i] ].cost = min(g[ g[pos].e[i] ].cost, g[pos].cost + 1);
    bfs(g[pos].e[i]);
  }
}

int main(void) {

  int n;
  cin >> n;
  g.resize(n);
  int u, k;
  for(int i=0; i<n; i++){
    cin >> u >> k;
    u--;
    g[u].v = u;
    g[u].e.resize(k);
    for(int j=0; j<k; j++){
      cin >> g[u].e[j];
      g[u].e[j]--;
    }
    sort(g[u].e.begin(), g[u].e.end());
  }

  g[0].cost = 0;
  bfs(0);
  for(int i=0; i<n; i++) cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl;

  // for(int i=0; i<n; i++) dfs(i);
  // for(int i=0; i<n; i++){
  //   cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl;
  // }



  return 0;
}

// EOF

C++: #include <iostream>
#include <stdio.h>
#include <queue>
 
using namespace std;
#define N 100
#define INF 2000000
 
int n, M[N][N];
int d[N];
 
void bfs(int s){
    int i,u,v;
 
    queue<int> q;
    q.push(s);
    for(i = 0; i < n; i++){
        d[i] = INF;
    }
 
    d[s] = 0;
 
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(v = 0; v < n; v++){
            if(M[u][v] == 0) continue;
            if(d[v] != INF) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(i = 0; i < n; i++){
        if(d[i] == INF){
            printf("%d -1\n", i+1);
        }else{
            printf("%d %d\n", i+1, d[i]);
        }
    }
}
 
 
int main(void){
    int i,j,u,k,v;
 
    scanf("%d", &n);
 
    for(i = 0;i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
 
    for(i = 0; i < n; i++){
      cin >> u >> k;
        u--;
        for(j=0;j<k;j++){
          cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
 
    bfs(0);
 
    return 0;
}

C++: #include<iostream>
#include<stdio.h>

using namespace std;
int d[101];

int n;
int M[101][101];
int Q[101];
int head = 0;
int tail = 0;

void enqueue(int x) {
	Q[tail] = x;
	if (tail == (tail - head)) {
		tail = 1;
	}
	else {
		tail = tail + 1;
	}
}

void BFS(int s) {
	enqueue(s);
	d[s] = head;
	while (head != tail) {
		s = Q[head];
		head = head + 1;
		
		for (int j = 1; j < n + 1; j++) {
			if (M[s][j] == 1) {
				if(d[j] == -1){
					d[j] = d[s] + 1;
					enqueue(j);
				}

			}
		}

	}
}

int main() {
	int  u, k, v;//頂点数、頂点の番号、出次数、隣接する頂点の番号
	

	cin >> n;



	//初期化
	for (int i = 0; i < 101; i++) {
		d[i] = -1;
		
		for (int j = 0; j < 101; j++) {
			M[i][j] = 0;

		}
	}


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


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

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1};
int dx[] = {1, -1, 0, 0};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int table[n + 1][n + 1]; rep(i, n + 1) rep(j, n + 1) table[i][j] = 0;
    rep(j, n) {
        int u, k; cin >> u >> k;
        int v;
        rep(l, k) {
            cin >> v;
            table[u][v] = 1;
        }
    }
    int mc[n + 1]; rep(i, n + 1) mc[i] = inf;
    queue<int> q;
    q.push(1);
    mc[1] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        rrep(i, 2, n + 1) {
            if (table[now][i] == 1 && mc[i] == inf) {
                mc[i] = mc[now] + 1;
                q.push(i);
            }
        }
    }
    rrep(i, 1, n + 1) {
        cout << i << " " << (mc[i] == inf ? -1 : mc[i]) << endl;
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

for(int a=0;a<n;a++){
    if (D[n]==0){
        D[n]=-1;
}
    D[0]=0;
    cout<<a+1<<" "<<D[a]<<endl;
}
    return 0;
}

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

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == -1) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = -1;
    }

    bfs(0);
    for (int i = 0; i < n; i++) {
        std::cout << i + 1 << " " << D[i] << std::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;
}