Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

struct Graph {
    enum Color {WHITE, GRAY, BLACK};
    vector<vector<int>> adj;
    vector<Color> color;
    vector<int> dist;
    Graph(int n) {
        adj.assign(n, vector<int>());
        color.assign(n, WHITE);
        dist.assign(n, -1);
    }
};

int main()
{
    int n;
    cin >> n;
    Graph g(n);
    for (int i = 0; i < n; i++) {
        int id, k;
        cin >> id >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            g.adj[id - 1].push_back(v - 1);
        }
    }

    queue<int> q;
    g.color[0] = Graph::GRAY;
    g.dist[0] = 0;
    q.push(0);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v : g.adj[u]) {
            if (g.color[v] == Graph::WHITE) {
                g.color[v] = Graph::GRAY;
                g.dist[v] = g.dist[u] + 1;
                q.push(v);
            }
        }
        g.color[u] = Graph::BLACK;
    }

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

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

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

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:-1;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==-1){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

int main(){
	int n,u,k,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			adj[u-1].push_back(v);
		}
	}
	bfs(n,0);
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1000
bool M[N][N];

int main(){
    int n,u,k,v;
    queue<int> Q;

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> u >> k;
        u--;
        for(int j=0;j<k;j++){
            cin >> v;
            v--;
            M[u][v]=true;
        }
    }

    int d[N]={0};
    Q.push(0);
    while(!Q.empty()){
        u=Q.front();
        Q.pop();
        for(int i=0;i<n;i++){
            if(M[u][i] && d[i]==0){
                d[i]=d[u]+1;
                Q.push(i);
            }
        }
    }

    for(int i=0;i<n;i++){
        printf("%d %d\n",i+1,d[i]);
    }

    return 0;
}

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

int n;
vector<int> G[MAX];
int d[MAX];

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		d[i] = -1;
	}
	for (int i = 0, u, k; i < n; i++) {
		cin >> u >> k;
		for (int j = 0, v; j < k; j++) {
			cin >> v;
			G[u - 1].push_back(v - 1);
		}
	}
	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		for (int &v : G[q.front()]) {
			if (d[v] == -1) {
				q.push(v);
				d[v] = d[q.front()] + 1;
			}
		}
		q.pop();
	}

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

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;

int n;
vector<vector<int>> hen;
vector<bool> seen;
vector<int> dist;

void bfs()
{
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    seen[0] = true;
    while(que.size())
    {
        auto cur = que.front();
        que.pop();
        for(auto i : hen[cur]){
            if(seen[i])continue;
            seen[i] = true;
            dist[i] = dist[cur] + 1;
            que.push(i);
        }
    }
}




int main()
{
    cin >> n;
    hen.resize(n);
    dist.resize(n, -1);
    seen.resize(n);
    for(int i = 0; i < n; i++){
        int u, k; cin >> u >> k; u--;
        for(int j = 0; j < k; j++){
            int tmp; cin >> tmp; tmp--;
            hen[u].push_back(tmp);
        }
    }
    bfs();

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


}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[10000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	  T[num].A[i]=0;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

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


  return 0;
}

C++: #include<bits/stdc++.h>

struct node{
    int d, flag;
    node(){
        d = 0;
        flag = 0;
    }
};

int main(void){
    int n;
    int ind, num, a;
    int time, ndi;

    std::cin >> n;
    std::vector<node> nd(n);
    std::vector<std::vector<int>> b(n, std::vector<int>(n));
    std::queue<int> st;

    for(int i = 0; i < n; i++){
        std::cin >> ind >> num;
        ind--;
        for(int i = 0; i < num; i++){
            std::cin >> a;
            a--;
            b[ind][a] = 1;
        }
    }

    time = 0;
    int x = 0;
    //for(int x = 0; x < n; x++){
        if(nd[x].flag == 0){
            ndi = x;
            time++;
            nd[x].flag = 1;
        
            st.push(x);
        }
    
        

        while(st.empty() != 1){
            //std::cout << ndi+1 << " " << time << std::endl;
            //time++;
            int tmp = -1;
            int tmp2;
            for(int i = 0; i < n; i++){
                if(b[ndi][i] == 1 && nd[i].flag == 0){
                    tmp = i;
                    tmp2 = x;
                    break;
                }
            }
            if(tmp == -1){
                //std::cout << ndi+1 << " " << time << std::endl;

                nd[ndi].flag = 1;
                st.pop();
                if(st.empty() != 1){
                    ndi = st.front();
                }

                continue;
            }

            nd[tmp].flag = 1;
            nd[tmp].d = nd[ndi].d + 1;
            st.push(tmp);
            
        }
    //}
    for(int i = 0; i < n; i++){
        if(nd[i].flag == 0 && i != 0){
            nd[i].d = -1;
        }
    }
    for(int i = 0; i < n; i++){
        std::cout << i+1 << " " << nd[i].d << std::endl;
    }

    return 0;
}