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;

typedef pair<int, int> P;
typedef long long int LL;

vector<int> v[101];
vector<int> dist;
queue<int> q;

int bfs() {
    dist[1] = 0;
    q.push(1);

    while (!q.empty()) {
        int c = q.front();
        q.pop();
        for (int i = 0; i < v[c].size(); i++) {
            if (dist[v[c][i]] == -1) {
                dist[v[c][i]] = dist[c] + 1;
                q.push(v[c][i]);
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    dist.resize(n + 1, -1);

    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int a;
            cin >> a;
            v[u].push_back(a);
        }
    }

    bfs();

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

    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using qi = queue<int>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

qi que;
vi dist;

void bfs(Graph &G) {
    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next_v : G[v]) {
            if (dist[next_v] != -1) continue;
            que.push(next_v);
            dist[next_v] = dist[v] + 1;
        }
    }
}

int main() {
	cin.tie(0);
   	ios::sync_with_stdio(false);

    int n; cin >> n;
    Graph G(n);
    REP(i, n) {
        int u; cin >> u;
        int k; cin >> k;
        REP(j, k) {
            int v; cin >> v;
            G[i].push_back(v-1);
        }
    }

    que.push(0);
    dist.assign(n, -1);
    dist[0] = 0;
    bfs(G);

    REP(i, n) {
        cout << (i+1) << " " << dist[i] << "\n";
    }
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
vector<vector<int> >vec;
int n;
vector<int>d;
void bfs(int s) {
	queue<int>q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		rep(v, 0, vec[u].size()) {
			if (d[vec[u][v]] != INF)continue;
			d[vec[u][v]] = d[u] + 1;
			q.push(vec[u][v]);
		}
	}
	rep(i, 0, n) {
		cout << i + 1 << " " << d[i] << endl;
	}
}
signed main() {
	cin >> n; vec = vector<vector<int> >(n); d = vector<int>(n, INF);
	rep(i, 0, n) {
		int u, k; cin >> u >> k; u--;
		rep(j, 0, k) {
			int v; cin >> v; v--;
			vec[u].push_back(v);
		}
	}
	bfs(0);

	return 0;
}

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

int main(){
	int n;
	cin >> n;
	vector<int> G(n,-1);
	vector<vector<int>> vs(n);
	int u,k,v;
	for(int loop=0;loop<n;loop++){
		cin >> u >> k;
		for(int loop2=0;loop2<k;loop2++){
			cin >> v;
			vs[u-1].push_back(v-1);
		}
	}
	queue<pair<int,int>> Q;
	Q.push(make_pair(0,0));
	G[0]=0;
	int lk,dist;
	while(!Q.empty()){
		lk = Q.front().first;
		dist = Q.front().second;
		Q.pop();
		for(int loop=0;loop<vs[lk].size();loop++){
			if(G[vs[lk][loop]]==-1){
				G[vs[lk][loop]]=dist+1;
				Q.push(make_pair(vs[lk][loop],dist+1));
			}
		}
	}
	for(int loop=0;loop<n;loop++){
		cout << loop+1 << " " << G[loop] << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int n,g[100][100],f[100],c1[100],c2[100];
void bfs(int now,int far){
  for(int i=0;i<n;i++) if(g[now][i]){
      if(f[i]<far+1){
        c1[i]=1;
        continue;
      }
      f[i]=far+1;
    }
  for(int i=0;i<n;i++) if(g[now][i]&&c1[i]!=1) bfs(i,far+1);
}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    if(i) f[i]=1000;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

  for(int i=0;i<n;i++) if(f[i]==1000) f[i]=-1;
  for(int i=0;i<n;i++) cout<<i+1<<" "<<f[i]<<endl;

  return 0;

}