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 <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge
{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
//using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;
const double PI = acos(-1.0);

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N]; // 距離で訪問状態(color)を管理する

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()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

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

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

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

C++: #include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

#define NMAX 100
#define INF -1
#define NIL -1

int n;
int d[NMAX + 1]; // distance, 1-origin
vector<int> adj[NMAX + 1];
queue<int> Q;

int next_unvisited_adj(int u) {
    vector<int>::iterator it;
    for (it = adj[u].begin(); it != adj[u].end(); it++) {
        if (d[*it] == INF)
            return *it;
    }
    return NIL;
}

void bfs(int x) {
    d[x] = 0;
    Q.push(x);
    int u, v;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();
        while ((v = next_unvisited_adj(u)) != NIL) {
            d[v] = d[u] + 1;
            Q.push(v);
        }
    }
}

int main() {
    scanf("%d", &n);
    int u, k, v;
    for (int i = 1; i <= n; i++) {
        d[i] = INF;
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            adj[u].push_back(v);
        }
    }
    bfs(1);
    for (int i = 1; i <= n; i++)
        printf("%d %d\n", i, d[i]);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY -1

int n, G[MAX][MAX], d[MAX];

void bfs(int s) {
	queue<int> Q;
	Q.push(s);
	d[s] = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++) {
			if (G[u][v] == 0) continue;
			if (d[v] != INFTY) continue;
			d[v] = d[u] + 1;
			Q.push(v);
		}
	}
}

int main(void) {
	int i, j, u, k, v;
	for (i = 0; i < MAX; i++) {
		d[i] = INFTY;
	}
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u >> k;
		for (j = 0; j < k; j++) {
			cin >> v;
			G[u][v] = 1;
		}
	}

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

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

#define MAX 100
static const int WHITE = 0; // not visit
static const int GRAY = 1; // visited
static const int BLACK = 2; // searched

int n;
int M[MAX][MAX] = { 0 };
int distance[MAX] = { 0 }; // ?????????????§??????????????????¢


void breadthFirstSearch() {
	queue<int> vertices;
	int v_states[MAX] = { WHITE };

	vertices.push(0); // ?§????
	while (!vertices.empty()) {
		int u = vertices.front();
		v_states[u] = GRAY;
		vertices.pop();
		for (int v = 0; v < n; v++) {
			if (M[u][v] == 1 && v_states[v] == WHITE) {
				::distance[v] = ::distance[u] + 1;
				vertices.push(v);
				v_states[v] = GRAY;
			}
		}
	}
	return;
}

int main() {
	cin >> n;

	for (int i = 0; i < n; i++) {
		int k, u;
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	breadthFirstSearch();
	for (int i = 0; i < n; i++) {
		cout << i + 1 << ' ' << ::distance[i] << endl;
	}

	return 0;

}