Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define pd(a,n) for(int i=0;i<n;i++)cout<<a[i]<<((i==n-1)?"\n":" ");cout<<endl;
#define pdd(a,n,m)for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define mp make_pair
#define pb push_back
#define ll long long
//#define __int64 lng long
#define P pair<int,int>
#define PP pair<P,int>

vector<int>G[111];
int dist[111];
int n;

int main(){
  cin>>n;
  rep(i,n)dist[i]=-1;

  rep(i,n){
    int a,k;
    cin>>a>>k;
    a--;
    rep(i,k){
      int x;
      cin>>x;
      x--;
      G[a].pb(x);
    }
    sort(G[a].begin(),G[a].end());
  }
  queue<P>bfs;
  bfs.push(mp(0,0));

  while(!bfs.empty()){
    int now=bfs.front().first;
    int cost=bfs.front().second;
    bfs.pop();
    if(dist[now]!=-1)
      continue;
    dist[now]=cost;
    for(int i=0;i<G[now].size();i++){
      int next=G[now][i];
      bfs.push(mp(next,cost+1));
    }
  }
  rep(i,n)cout<<i+1<<" "<<dist[i]<<endl;
  return 0;
}

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

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

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

int main(){
 cin >> n;
 int u, k;
 int v[100000];
 for(int i=0; i<n; i++){
  cin >> u >> k;
  for(int j=0; j<k; j++){
   cin >> v[j];
   G[i+1][v[j]] = 1;
  }
 }
 bfs(1);
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
vector<vector<int>> G(100);
vector<int> d(100);
const int INF = 1<<28;
void bfs(){
    queue<int> Q;
    Q.push(0);
    fill(d.begin(),d.end(),INF);
    d[0]=0;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        for(auto a:G[u]){
            if(d[a]!=INF) continue;
            d[a]=d[u]+1;
            Q.push(a);
        }
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=0;i<n;++i){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;++j){
            int v;
            cin>>v;
            G[u-1].push_back(v-1);
        }
    }
    bfs();
    for(int i=0;i<n;++i)
        cout<<i+1<<' '<<(d[i]==INF?-1:d[i])<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MK make_pair 
typedef pair<int,int> pi;


int main(){
  const int MX=1e9+7;
  int a[1000][1000];
  fill(&a[0][0],&a[999][1000],MX);
  for(int i=0;i<1000;i++){
    a[i][i]=0;
  }
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int w;
    cin>>w;
    int m;
    cin>>m;
    for(int t=0;t<m;t++){
      int e;
      cin>>e;
      a[w][e]=1;
    }
  }
  for(int i=1;i<=n;i++){
    for(int t=1;t<=n;t++){
      for(int k=1;k<=n;k++){
	a[t][k]=min(a[t][k],a[t][i]+a[i][k]);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<(a[1][i]==MX? -1:a[a][i])<<endl;
  }


  
  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
vector<int> d[120];
int dist[120];
bool visit[120];
void bfs(){
	queue<pii> que;
	que.push(mp(1,0));
	while(!que.empty()){
		pii p =que.front();
		que.pop();
		int pos =p.first;
		int cnt =p.second;
		if(visit[pos])continue;
		//cout <<pos<<" "<<cnt<<endl;
		visit[pos]=true;
		dist[pos]=cnt;
		for(int i =0;i <d[pos].size();i++){
			if(!visit[d[pos][i]]){
				que.push(mp(d[pos][i],cnt+1));
			}
		}
	}
}

int main(){
	cin>>n;
	memset(dist, -1, sizeof(dist));
	memset(visit, false, sizeof(false));
	for(int i =0;i < n;i++){
		int u,k;
		cin>>u>>k;
		for(int j = 0;j <k;j++){
			int v;
			cin>>v;
			d[u].push_back(v);
		}
	}
	bfs();
	for(int i =1;i <=n;i++){
		cout <<i<<" "<<dist[i]<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#define MAX 1000
using namespace std;

int main()
{
    int D[101];
    queue<int> Q;
    int n,i,j,u,k;
    for(i=0;i<101;i++)
        D[i]=0;
    cin >> n;
    int Adj[n][n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            Adj[i][j]=0;
    for(i=0;i<n;i++)
    {
        cin >> u >> k;
        int v[k];
        for(j=0;j<k;j++)
        {
            cin >> v[j];
            Adj[u-1][v[j]-1]=1;
        }
    }
    D[0]=0;
    Q.push(0);
    while(!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for(i=0;i<n;i++)
        {
            if(Adj[cur][i]==1&&D[i]==0)
            {
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    cout << 1 << ' ' << D[i] << endl;
    for(i=1;i<n;i++)
    {
        if(D[i]==0)
            cout << i+1 << ' ' << -1 << endl;
        else    cout << i+1 << ' ' << D[i] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
// Welcome to my source code!

const int INF = 1e9;

int n;
vector<int> g[105];
int d[105];

void bfs() {
    queue<int> que;
    que.push(0);
    d[0] = 0;
    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (auto v : g[u]) {
            int t = d[v];
            d[v] = min(d[v], d[u] + 1);
            if (t != d[v]) que.push(v);
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0, u, k; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int i = 0, v; i < k; i++) {
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    for (int i = 0; i < n; i++) d[i] = INF;
    bfs();
    for (int i = 0; i < n; i++) {
        cout << i + 1 << ' ';
        if (d[i] == INF) cout << -1;
        else cout << d[i];
        cout << endl;
    }
}