Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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

using namespace std;


int main(){
    int n;
    cin >> n;
    vector<vector<int> > adj(110);
    int D[110];
    for(int i = 0; i < n; i++){
        int u, k;
        cin >> u >> k; 
        D[u] = -1;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            adj[u].push_back(v);
        }
    }
    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    while(!Q.empty()){
        int cur = Q.front(); 
        Q.pop();
        for(int i = 0; i < adj[cur].size(); i++){
            int dst = adj[cur][i];
            if(D[dst] == -1){
                D[dst] = D[cur]+1;
                Q.push(dst);
            }
        }
    }
    for (int i = 1; i < n+1; i++){
        cout << i << " " << D[i] << endl;
    }
}

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

using namespace std;
const static int N=100;
const static 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;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int v=0; v<n; v++){
            if(M[u][v]==0){continue;}
            if(d[u]!=0){continue;}
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}

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

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

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

#define rep(i,n)for(int i=0;i<n;i++)

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

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    rep(i,n)d[i]=INFTY;
    d[s]=0;
    int u;
    while (!q.empty())
    {
        u=q.front();
        q.pop();
        rep(v,n)
        {
            if(M[u][v]==0)continue;
            if (d[v]!=INFTY)continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    rep(i,n)
    {
        cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
    }
}

int main()
{
    int u,k,v;

    cin>>n;
    rep(i,n)
    {
        rep(j,n)M[i][j]=0;
    }
    
    rep(i,n)
    {
        cin>>u>>k;
        u--;
        rep(j,k)
        {
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

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

    vector<vector<ll>> g(n);

    rep(i, n){
        int u, k;
        cin >> u >> k;

        rep(j, k){
            int v;
            cin >> v;
            v--;
            g[i].push_back(v);
        }
    }

    queue<int> que;
    que.push(0);

    ll dist[n];
    rep(i, n)dist[i] = -1;
    dist[0] = 0;

    bool visited[n] = {};
    visited[0] = true;

    while(!que.empty()){
        int pos = que.front();

        que.pop();

        rep(i, g[pos].size()){
            int child = g[pos][i];

            if(visited[child])continue;
            visited[child] = true;

            dist[child] = dist[pos] + 1;

            que.push(child);
        }
    }

    rep(i, n)cout << i+1 << " " << dist[i] << endl;
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
  
class Queue{
private:
  int inf[MAX];
  int head,tail;
   
public:
   
  Queue(){
    syokika();
  }
   
  void syokika(){
    head = 0;
    tail = 0;
  }
   
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
   
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
   
  int front(){
    return inf[head];
  }
   
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
   
  int size(){
    return tail - head;
  }
   
};
  
int imap[200][200];
int k[200] = {0};
int ans[200] = {0};
bool used[200];
  
int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;
  
  for(int i=0;i<200;i++){
    k[i]=ans[i]=u[i]=-1;
  }
  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
  
   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j;
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }
  
  Queue que = Queue();
  
  que.push(u[0]);
  ans[u[0]]=0;
  
  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
    que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
    used[imap[p][i]] = true;
    ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
   
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ans[u[i]] << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int m[101][101],n;
int d[101]; //最短距離を記録
void bfs(int s){
  queue<int>q;
  q.push(s); //始点sを入れる（訪問）
  for(int i=0;i<n;i++) d[i]=INF;//予めINFを入れておく
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop(); //キューから頂点uを取り出し、訪問
    for(int v=0;v<n;v++){
      if(m[u][v]==0) continue;
      //辺がない
      if(d[v]!=INF) continue;
      //訪問したことがある
      d[v]=d[u]+1; //vは未訪問の頂点であり、隣接しているため、d[u]に1を足す
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  }
}
int main(){
  int u,k,v;
  scanf("%d",&n);
   for(int i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(int j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      m[u][v]=1;
    }
  }
   bfs(0);
   return 0;
}