Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> G[110], d(110);

int main() {
    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);
        }
    }

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

    queue<int> que;
    que.push(0);
    d[0] = 0;
    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (int v: G[u]) {
            if (d[v] == -1) {
                que.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

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

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define reptr(i,n) for(int (i)=(n);(i)>=0;--(i))
#define All(x) (x).begin(),(x).end()
const ll MOD=1e9+7;
using namespace std;

vector<int> ans;
int state[105][105]={};
int n;
void bfs(int s){
  queue<int> q;
  q.push(s);
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      //隣接していない
      if(state[u][v]==false) continue;
      //既に探索されている
      if(ans[v]!=INT_MAX) continue;
      //未探索の場合
      ans[v] = ans[u]+1;
      q.push(v);
    }
  }

  rep(i,n){
    if(ans[i]==INT_MAX) ans[i] = -1;
    cout << i+1 << " " << ans[i] << endl;
  }
}

int main(){
  cin >> n;
  ans.resize(n,INT_MAX);
  ans[0]=0;
  rep(i,n){
    int u,k;
    cin >> u >> k;
    u--;
    rep(j,k){
      int v;
      cin >> v;
      v--;
      state[u][v]=1;
    }
  }
  bfs(0);
}

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

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == 0) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

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

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