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 <vector>
#include <queue>
#include <algorithm>

using namespace std;
using Graph = vector<vector<int>>;

int main() {

    int n; cin >> n;
    int u, k;

    Graph G(n+1);
    for ( int i = 1; i <= n; ++i ) {
        cin >> u >> k;
        for ( int j = 1; j <= k; ++j ) {
            int a;
            cin >> a;
            G[i].push_back(a);
            //G[b].push_back(a);
        }
    }

    /*for ( int i = 1; i <= n; ++i ) {
        for ( int ax : G[i] ) {
            cout << ax ;
        }
        cout << endl;
    }*/

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

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

    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 = 1; i <= n; ++i ) {
        cout << i << " " << dist[i] << endl; 
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
constexpr ll MOD = 1e9+7;
constexpr ll INF = 1ll<<60;

int main(int argc, char **argv)
{
	ll n; cin >> n;
	Graph G(n);
	for (ll i = 0; i < n; ++i)
	{
		ll a, k; cin >> a >> k;
		--a;
		for (ll j = 0; j < k; ++j)
		{
			ll b;
			cin >> b;
			G[a].push_back(--b);
		}
	}

	vector<ll> d(n, -1);
	d[0] = 0;
	queue<ll> que;
	que.push(0);

	while (!que.empty())
	{
		ll now = que.front(); que.pop();

		for (auto &nv : G[now])
		{
			if (d[nv] != -1) continue;
			d[nv] = d[now] + 1;
			que.push(nv);
		}
	}

	for (ll i = 0; i < n; ++i)
		std::cout << i + 1 << " " << d[i] << std::endl;
}

C++: //
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node{
  int u,k,v[99];
  int distance;
};

int main(int argc, char *argv[])
{
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for(int i=0; i<n; i++){
    cin >> node[i].u >> node[i].k;

    for(int j=0; j<node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance=-1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while(!que.empty()){
    Node fro = que.front();

    for(int i=0; i<fro.k; i++){
      int x = fro.v[i]-1;
  
      if(node[x].distance==-1){
	    node[x].distance = fro.distance+1;

        que.push(node[x]);
        }
    }

    que.pop();
  }

  for(int i=0; i<n; i++)
    cout << node[i].u << " " << node[i].distance << endl;

}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define fi first
#define se second

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 x;
            cin>>x;
            G[u-1].push_back(x-1);
        }
    }

    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 nv : G[v]){
            if(dist[nv]!=-1)continue;
            que.push(nv);
            dist[nv]=dist[v]+1;
        }
    }

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



    return 0;
}

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

int n;
char c[101];
int g[101][101];
int t = 0;
int visit[101];
int finish[101];
int d[101];

void bfs(int start) {
    t++;
    queue<int> q;
    q.push(start);
    c[start] = 'g';
    visit[start] = t;
    while(!q.empty()) {
        t ++;
        int now = q.front();
        for (int i = 1; i <= n ; ++i) {
            if(g[now][i] && c[i] == 'w') {
                q.push(i);
                c[i] = 'g';
                visit[i] = t;
            }
        }
        q.pop();
        c[now] = 'b';
        finish[now] = t;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n ; ++i) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            g[u][v] = 1;
        }
    }

    for (int l = 0; l < n; ++l) {
        c[l+1] = 'w';
    }

    for (int i1 = 1; i1 <=n ; ++i1) {
        d[i1] = -1;
    }
    for (int m = 1; m <= n ; ++m) {
        d[1] = 1;
        if(c[m] == 'w') bfs(m);
        break;

    }

    return 0;
}

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

#define MAX_NODE 100
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : INF);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == INF) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

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

	bfs(n, 0);

	for (int i = 0; i < n; i++)
		if (d[i] != INF)
			cout << i + 1 << " " << d[i] << endl;
		else
			cout << i + 1 << " -1" << endl;

	return 0;
}