Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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