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 <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<vector<int> > edge(n,vector<int>());
	REP(i,n){
		int v;cin>>v;v--;int t;cin>>t;
		while(t--){
			int u;cin>>u;u--;
			edge[v].push_back(u);
		}
	}
	queue<pair<int,int> > que;
	int mindist[103];
	fill_n(mindist,n,INF);
	mindist[0] = 0;
	que.push(make_pair(0,0));
	while(!que.empty()){
		pair<int,int> v = que.front();
		que.pop();
		v.second++;
		REP(i,edge[v.first].size()){
			if(v.second < mindist[edge[v.first][i]]){
				mindist[edge[v.first][i]] = v.second;
				que.push(make_pair(edge[v.first][i],v.second));
			}
		}
	}
	REP(i,n){
		cout << i+1 << ' ';
		if(mindist[i]==INF)cout << -1 << endl;
		else	cout << mindist[i] << endl;
	}
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;
    vector< vector<int> > edges(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        edges[u].resize(k);
        REP(j, k) {
            cin >> edges[i][j];
            edges[i][j]--;
        }
    }

    queue<int> que;
    vector<int> dist(N, -1);
    dist[0] = 0;
    que.push(0);
    while(!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next: edges[v]) {
            if(dist[next] == -1) {
                dist[next] = dist[v] + 1;
                que.push(next);
            }
        }
    }

    REP(i, N) cout << i+1 << " " << dist[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,M[101][101];
int d[101];

void bfs(int s){
  
  queue<int>q;
  
  q.push(s);
  
  for(int i=0;i<n;i++) d[i]=INF;
  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[v]!=INF) continue;
      d[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;
  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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
int n;
vector<int> graph[110];
int visited[110],dist[110];
int main()
{
    cin >>n;
    rep(i,n){
        int u;
        
        cin >> u;
        u--;
        int k;
        cin >> k;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            graph[u].push_back(v);
            
        }

    }
    queue<int> q;
    
    q.push(0);
    visited[0] = 1;
    while(!q.empty()){
        auto now = q.front();q.pop();
        
        for(auto next :graph[now]){
            if(visited[next])continue;
            dist[next] = dist[now] +1;
            q.push(next);
            visited[next] = 1;
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

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

    Graph G(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);
        }
    }

    vector<int> dist(n, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

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

        for(int nv: G[v]){
            if(dist[nv] != -1) continue;

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

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