Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;


bool is_connected[150][150];
int min_distance[150];

void init() {
    int i, j;

    rep(i, 150) {
        min_distance[i] = 150;
    }
    min_distance[0] = 0;
    min_distance[1] = 0;
    rep(i, 150) rep(j, 150) is_connected[i][j] = false;
}

void solveDepth(int src_id, int cur_depth) {
    int dst_id;
    rep(dst_id, 150) {
        if (is_connected[src_id][dst_id]) {
            if (cur_depth + 1 < min_distance[dst_id]) {
                min_distance[dst_id] = cur_depth + 1;
                solveDepth(dst_id, cur_depth + 1);
            }
        }
    }
}

int main() {
    int n, i, j, u, v, k;
    cin >> n;

    init();

    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v;
            is_connected[u][v] = true;
        }
    }

    solveDepth(1, 0);

    rep(i, n) {
        cout << i + 1 << " ";
        if (min_distance[i + 1] == 150) {
            cout << -1 << endl;
        } else {
            cout << min_distance[i + 1] << endl;
        }
        
    }

    return 0;
}