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>
#include<queue>

using namespace std;

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

int n, M[N][N];
int d[N];  //距離で訪問状態(color)を管理する

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++){
    cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);

  return 0;
}

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

int main()
{
	int i,j,n,k,e[101][101],v[101],a,b,d[101],s;
	queue<int> Q;
	
	for(i=0;i<=100;i++)
	{
		for(j=0;j<=100;j++)
		{
			e[i][j]=0;
		}
		v[i]=0;
		d[i]=-1;
	}
	scanf("%d",&n);
	for(i=0;i<=n-1;i++)
	{
		scanf("%d%d",&a,&k);
		for(j=0;j<=k-1;j++)
		{
			scanf("%d",&b);
			e[a][b]=1;
		}
	}
	
	Q.push(1);
	d[1]=0;
	s=1;
	v[1]=1;
	while(Q.empty()!=1)
	{
		i=Q.front();
		for(j=1;j<=n;j++)
		{
			if(e[i][j]==1 && v[j]==0)
			{
				d[j]=d[i]+1;
				v[j]=1;
				Q.push(j);
			}
		}
		Q.pop();
	}
	for(i=1;i<=n;i++)
	{
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
void bfs(int);
int n,M[N][N],d[N],u,i,j;
string color[N];
int main(){
  int inu,k,v;
  cin >> n;
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      M[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    cin >> inu >> k;
    for(j=0;j<k;j++){
      cin >> v;
      M[inu][v]=M[v][inu]=1;
    }
  }
  bfs(1);
  for(i=1;i<=n;i++){
    if(d[i]>n) cout << i << ' ' << -1 << endl;
    else cout << i << ' ' << d[i] << endl;
  }
  return 0;
}
void bfs(int s){
  queue<int> q;
  for(i=1;i<=n;i++){
    color[i]="WHITE";
    d[i]=1000000000;
  }
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=true){
    u=q.front();
    q.pop();
    for(i=1;i<=n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
}

C++: #include <bits/stdc++.h>
#define log(x) cout<<x<<endl
#define pb push_back
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long lint;
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;

int gcd(lint x, lint y){
  return y==0 ? x:gcd(y,x % y);
}

int digsum(int n) {
  int res = 0;
  while(n > 0) {
    res += n%10;
    n /= 10;
  }
  return res;
}

int dignum(int n) {
  int res = 0;
  while(n > 0) {
    res++;
    n /= 10;
  }
  return res;
}

int stringcount(string s, char c) {
  return count(s.cbegin(), s.cend(), c);
}

int lcm(lint x, lint y){
  return x/ gcd(x, y) * y;
}

int combi(int n, int r){
  int num = 1;
  for(int i = 1; i <= r; i++){
    num = num * (n - i + 1) / i;
  }
  return num;
}

int main(){
  int n;
  cin>>n;
  Graph g(n);
  for(int i=0;i<n;i++){
    int num,m;
    cin>>num>>m;
    num--;
    for(int j=0;j<m;j++){
      int a;
      cin>>a;
      a--;
      g[num].pb(a);
    }
  }
  vector<int> dist(n,-1);
  queue<int> que;
  dist[0]=0;
  que.push(0);

  while(!que.empty()){
    int x=que.front();
    que.pop();
    for(int nv:g[x]){
      if(dist[nv]!=-1) continue;

      dist[nv]=dist[x]+1;
      que.push(nv);

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



}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ' <<d[0][i] << endl;
 }
 
  return 0;
}

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

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

  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--;
      a[u][v] = 1;
    }
  }

  queue<int> q;
  int d[n];
  for (int i = 0; i < n; i++) d[i] = INF;
  q.push(0);
  d[0] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (a[u][i] == 0) continue;
      if (d[i] != INF) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }

  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
}

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

using namespace std;

void bfs(int v,vector<vector<int>>& vertex, int d[]);

int depth = 0;

int main(){
    int n;
    cin >> n;
    int num;
    int tmp2;
    int d[n];
    for(int i=0;i<n;i++){
        d[i] = -1;
    }
    vector<vector<int>> vertex;
    vector<int> tmp;
    for(int i=0;i<n;i++){
        cin >> num;
        cin >> num;
        for(int j=0;j<num;j++){
            cin >> tmp2;
            tmp.push_back(tmp2-1);
        }
        vertex.push_back(tmp);
        tmp.clear();
    }
    bfs(0, vertex, d);
    
    for(int i=0;i<n;i++){
        cout << i+1 << " " <<  d[i] << endl;
    }
    return 0;
}

void bfs(int v, vector<vector<int>>& vertex, int d[]){
    queue<int> Q;
    d[v] = depth++;
    Q.push(v);
    while(!Q.empty()){
        v = Q.front();
        Q.pop();
        for(int i : vertex[v]){
            if(d[i]==-1){
                d[i] = depth;
                Q.push(i);
            }
        }
        depth++;
    }
}