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

using namespace std;

void bfs(int v,vector<vector<int>>& vertex, int d[]);

//int depth = 0;

int main(){
    int n;
    cin >> n;
    int num;
    int tmp2;
    int d[n];
    for(int i=0;i<n;i++){
        d[i] = -1;
    }
    vector<vector<int>> vertex;
    vector<int> tmp;
    for(int i=0;i<n;i++){
        cin >> num;
        cin >> num;
        for(int j=0;j<num;j++){
            cin >> tmp2;
            tmp.push_back(tmp2-1);
        }
        vertex.push_back(tmp);
        tmp.clear();
    }
    bfs(0, vertex, d);
    
    for(int i=0;i<n;i++){
        cout << i+1 << " " <<  d[i] << endl;
    }
    return 0;
}

void bfs(int v, vector<vector<int>>& vertex, int d[]){
    queue<int> Q;
    Q.push(v);
    d[v] = 0;
    while(!Q.empty()){
        v = Q.front();
        Q.pop();
        bool flag = false;
        for(int i : vertex[v]){
            if(d[i]==-1){
                d[i] = d[v] + 1;
                Q.push(i);
                flag = true;
            }
        }
    }
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if(graph[u][i] == 0 continue;
      if(d[i] != no) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}

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

struct{
	int link;
	int stg;
}typedef LinkStage;

class Vertex{
	private:
		int* links;
		int index;
		int num;
		bool arrived;
		int id;
		int d;
	public:
		Vertex(int u,int k):id(u), num(k),links(new int[k]),index(0),arrived(false),d(-1){}
		void addLink(int);
		void enQueueLinks(queue<LinkStage>*,int);	
		void print();
};

void Vertex::addLink(int link){
	links[index] = link - 1;
	index++;
}

void Vertex::enQueueLinks(queue<LinkStage>* taskQueue,int dist){
	if(arrived)
		return;

	for(int i = 0; i < index;i++){
		LinkStage stage;
		stage.link = links[i];
		stage.stg = dist + 1;
		taskQueue->push(stage);
	}
	arrived = true;
	d = dist;
}

void Vertex::print(){
	cout << id << " " << d << endl;
}

int main(){
	int n;
	cin >> n;
	Vertex** vertexes = new Vertex*[n];
	for(int i = 0;i < n;i++){
		int u;
		cin >> u;
		int k;
		cin >> k;
		Vertex* vertex = new Vertex(u,k);
		for(int j = 0;j < k;j++){
			int v;
			cin >> v;
			vertex->addLink(v);
		}
		vertexes[u - 1] = vertex;
	}
	queue<LinkStage> task;
	vertexes[0]->enQueueLinks(&task,0);
	while(!task.empty()){
		LinkStage curr = task.front();
		task.pop();
		int nextI = curr.link;
		int d = curr.stg;
		vertexes[nextI]->enQueueLinks(&task,d);
	}
	for(int i = 0; i < n;i++){
		vertexes[i]->print();
	
	}
	
	return 0;
}

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

int n;
vector<vector<int> > G;
vector<int> D;

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    D[src] = 0; // ?????????
    while(!Q.empty()){
        int cur = Q.front(); // ??????????´???????????????????
        Q.pop();
        for(int dst = 0; dst < n; ++dst){ // ???????????? dst ????????????
            if(G[cur][dst] == 1 && D[dst] == -1){ // cur??????dst???????????????,dst???????¨??????????
                D[dst] = D[cur] + 1;
                Q.push(dst); // dst????¨??????????????????????
            }
        }
    }
}

int main(){
    cin >> n;
    G = vector<vector<int> >(n, vector<int>(n, 0));
    D = vector<int>(n, -1);
    for(int i=0; i<n; ++i){
        int u;
        cin >> u;
        // 1~n???index?????¨????????????????????????????????????1????????????0~n-1????????????
        u -= 1;
        int k;
        cin >> k;
        for(int j=0; j<k; ++j){
            int v;
            cin >> v;
            v -= 1;
            G[u][v] = 1;
        }
    }
    bfs(0);
    for(int i=0; i<n; ++i){
        cout << i+1 << " " << D[i] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define pd(a,n) for(int i=0;i<n;i++)cout<<a[i]<<((i==n-1)?"\n":" ");cout<<endl;
#define pdd(a,n,m)for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define mp make_pair
#define pb push_back
#define ll long long
//#define __int64 lng long
#define P pair<int,int>
#define PP pair<P,int>

vector<int>G[111];
int dist[111];
int n;

int main(){
  cin>>n;
  rep(i,n)dist[i]=-1;

  rep(i,n){
    int a,k;
    cin>>a>>k;
    a--;
    rep(i,k){
      int x;
      cin>>x;
      x--;
      G[a].pb(x);
    }
    sort(G[a].begin(),G[a].end());
  }
  queue<P>bfs;
  bfs.push(mp(0,0));

  while(!bfs.empty()){
    int now=bfs.front().first;
    int cost=bfs.front().second;
    bfs.pop();
    if(dist[now]!=-1)
      continue;
    dist[now]=cost;
    for(int i=0;i<G[now].size();i++){
      int next=G[now][i];
      bfs.push(mp(next,cost+1));
    }
  }
  rep(i,n)cout<<i+1<<" "<<dist[i]<<endl;
  return 0;
}

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

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

 for(int i=1; i<=n; i++){
  if(i!=1 && D[i]==0 && i!=1){
   D[i]=-1;
  }
  cout << i << " " << D[i] << endl;
 }
}

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