Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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