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;

const int MAX=200;
vector<int> T[MAX],state(MAX),d(MAX,-1);
int N;
queue<int> Q;

void BFS(){
    Q.push(0);
    d[0]=0;
    state[0]=1;
    while(Q.size()){
        int a=Q.front();
        for(int i=0;i<T[a].size();i++){
            if(state[T[a][i]]==0){
            Q.push(T[a][i]);
            state[T[a][i]]=1;
            d[T[a][i]]=d[a]+1;
        }
        }
        Q.pop();
    }
    for(int i=0;i<N;i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}

int main(){

    cin>>N;
    
    for(int i=0;i<N;i++){
        state[i]=0;
    }
    for(int i=0;i<N;i++){
        int x,y;cin>>x>>y;
        x--;
    for(int j=0;j<y;j++){
        int a;cin>>a;
        a--;;
        T[x].push_back(a);
    }
    }
    BFS();
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
static const 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;
	int U;
	while (!Q.empty()) {
		U = Q.front(); Q.pop();
		for (int V = 0; V < n; V++) {
			if (M[U][V] == 0) continue;
			if (D[V] != INFTY) continue;
			D[V] = D[U] + 1;
			Q.push(V);
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((D[i] == INFTY) ? (-1) : D[i]) << endl;
	}
}
int main() {
	int U, K, V;

	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; j++) {
			cin >> V;
			V--;
			M[U][V] = 1;
		}
	}
	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    ll N;
    cin >> N;
    Graph graph(N);
    rep(i, N) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            graph[u].push_back(v);
        }
    }
    vector<ll> dist(N, -1);
    dist[0] = 0;
    queue<P> que;
    que.push({0, 0});
    while (!que.empty()) {
        P p = que.front();
        que.pop();
        for (ll v: graph[p.first]) {
            if (dist[v] == -1) {
                dist[v] = p.second + 1;
                que.push({v, dist[v]});
            }
        }
    }
    rep(i, N) {
        cout << i + 1 << ' ' << dist[i] << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
vector<vector<int>> M(100, vector<int>(100));
vector<int> C(100);
vector<int> D(100);


void bfs(int v) {
  C.at(v) = GRAY;
  D.at(v) = 0;
  queue<int> Q;
  Q.push(v);

  while (Q.size() > 0) {
    int u = Q.front(); Q.pop();
    C.at(u) = BLACK;
    rep(i, n) {
      if (M.at(u).at(i) == 1 && C.at(i) == WHITE){
        Q.push(i);
        C.at(i) = GRAY;
        D.at(i) = D.at(u)+1;
      } 
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int id, k; cin >> id >> k;
    rep(j, k) {
      int v; cin >> v;
      M.at(i).at(v-1) = 1;
    }
  }
  rep(i, n)D.at(i) = -1;
  bfs(0);
  rep(i,n) cout << i+1 << " " << D.at(i) << endl;
}

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

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif


int main(int argc, char **argv){
	int n, u, k, v;
	std::vector<std::vector<int> > G;
	std::vector<int> min_cost;
	std::vector<bool> is_searched;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n;
#else
	std::cin >> n;
#endif

	G.resize(n);
	is_searched.resize(n, false);
	min_cost.resize(n, -1);

	for(int i = 0; i < n; i++){
#if INPUT_FROM_FILE
		ifs >> u >> k;
#else
		std::cin >> u >> k;
#endif
		G[u - 1].resize(k);
		for(int j = 0; j < k; j++){
#if INPUT_FROM_FILE
			ifs >> v;
#else
			std::cin >> v;
#endif
			G[u - 1][j] = v - 1;
		}
	}

	min_cost[0] = 0;

	std::queue<int> que;
	
	que.push(0);
	while(!que.empty()){
		int n = que.front(); que.pop();

		for(int i = 0; i < G[n].size(); i++){
			int nn = G[n][i];
			
			if(min_cost[nn] < 0 || min_cost[nn] > min_cost[n] + 1){
				min_cost[nn] = min_cost[n] + 1;
				que.push(nn);
			}
		}
	}

	for(int i = 0; i < n; i++){
		std::cout << i + 1 << " " << min_cost[i] << std::endl;
	}


	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
bool g[110][110];
int d[110];

void bfs(int s)
{
	d[s] = 0;
	queue<int> q;
	q.push(s);

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (g[u][i] && d[i] == INF) {
				q.push(i);
				d[i] = d[u] + 1;
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			g[u][v] = true;
		}
	}

	for (int i = 1; i <= n; i++) d[i] = INF;

	bfs(1);

	for (int i = 1; i <= n; i++)
		cout << i << " " << d[i] << endl;

	return 0;
}