Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

// ------ Class ------ //
class Graph1 {
public:
	// ------ Variables ------ //
	static const int INF = 1000000000;
	int V, E; vector<vector<int> > G;

	// ------ Constructors ------ //
	Graph1() : V(0), E(0), G(vector<vector<int> >()) {}
	Graph1(int v) : V(v), E(0), G(vector<vector<int> >(v)) {}
	Graph1(vector<vector<int> > g) : V(g.size()), G(g) { for (int i = 0; i < g.size(); i++) E += g[i].size(); }

	// ------ Basic Functions ------ //
	int size() { return V; }
	void add1(int v1, int v2) { G[v1].push_back(v2); E++; }
	void add2(int v1, int v2) { add1(v1, v2); add1(v2, v1); }

	// ------ Operators ------ //
	friend bool operator==(const Graph1& g1, const Graph1& g2) { return g1.G == g2.G; }
	friend bool operator!=(const Graph1& g1, const Graph1& g2) { return g1.G != g2.G; }
	vector<int> operator[](int x) { return G[x]; }

	// ------ Algorithms ------ //
	vector<int> dist(int s) {
		queue<int> que; que.push(s);
		vector<int> d(V, INF); d[s] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i = 0; i < G[u].size(); i++) {
				if (d[G[u][i]] == INF) {
					d[G[u][i]] = d[u] + 1;
					que.push(G[u][i]);
				}
			}
		}
		return d;
	}
};

// ------ Main ------ //
int n, u, k, v;
int main() {
	cin >> n; Graph1 g(n);
	for(int i = 0; i < n; i++) {
		cin >> u >> k;
		for(int j = 0; j < k; j++) cin >> v, g.add1(u - 1, v - 1);
	}
	vector<int> d = g.dist(0);
	for(int i = 0; i < n; i++) cout << i + 1 << ' ' << (d[i] != Graph1::INF ? d[i] : -1) << endl;
}

C++: #include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;


int main(){
  int n;
  cin >> n;
  vvll edge(n, vll(0));
  for(int i = 0; i < n; i++){
    ll u, k;
    cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      ll v;
      cin >> v;
      v--;
      edge.at(u).push_back(v);
    }
  }

  vector<Pll> d(n);
  for(int i = 0; i < n; i++){
    d.at(i).first = INF;
    d.at(i).second = (ll)i;
  }
  d.at(0).first = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(make_pair(0,0));

  while(!que.empty()){
    P p = que.top(); que.pop();

    if(d.at(p.second).first < p.first) continue;

    for(int i = 0; i < edge.at(p.second).size(); i++){
      ll v = edge.at(p.second).at(i);
      if(d.at(v).first > p.first + 1LL){
        d.at(v).first = p.first + 1LL;
        que.push(d.at(v));
      }  
    }
  }

  for(int i = 0; i < n; i++){
    if(i == 0) continue;
    if(d.at(i).first == INF) d.at(i).first = -1;
  }

  for(int i = 0; i < n; i++){
    cout << i+1 << ' ' << d.at(i).first << endl;
  }

}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
typedef long long ll;

int main(){
	int n;
	cin >> n;
	vector<vector<int> > to(n);
	rep(i,n){
		int u;
		cin >> u;
		u--;
		int k;
		cin >> k;
		rep(j,k){
			int v;
			cin >> v;
			v--;
			to[u].push_back(v);
		}
	}
	
	int dist[105];
	rep(i,n){
		dist[i] = -1;
	}
	
	queue<int> que;
	dist[0] = 0;
	que.push(0);
	while(!que.empty()){
		int p = que.front();
		que.pop();
		for (int i = 0; i < to[p].size(); ++i) {
			int nextv = to[p][i];
			if(dist[nextv] != -1) continue;
			dist[nextv] = dist[p] + 1;
			que.push(nextv);
		}
	}
	
	rep(i,n){
		cout << i+1 << " " << dist[i] << endl;
	}
    return 0;
}

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

int n, d[110];
bool a[110][110] = {};

void bfs(int u){
  
  queue<int> Q;
  
  Q.push(u);
  d[u] = 0;
  
  while(Q.size()){
    int p = Q.front();
    Q.pop();
    
    for(int i = 2;i <= n;i++){
      if(a[p][i] && d[i] == 0){
        Q.push(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;
  
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);
  
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  return 0;
}

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

using namespace std;

const int N = 100;
const int Inf = 1e9;

int m[N][N];
int d[N];
int n;

void bfs(int);

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = 0;
    }
    d[i] = -1;
  }

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

  return 0;
}

void bfs(int s) {
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < n; ++i) {
      if (m[u][i] == 0 || d[i] != -1) {
        continue;
      }
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << '\n';
  }
}

C++: //By Vlgd
#include<bits/stdc++.h>
using namespace std;
int G[101][101],n,dis[101],vis[101]={0},cur=0;
queue<int>q;
void bfs(int u){
	q.push(u);
	vis[u]=1;
	dis[u]=0;
	while(!q.empty()){
		int w=q.front();q.pop();
		for (int v=1;v<=n;v++) 
			if (G[w][v]&&!vis[v]){ 
				dis[v]=dis[w]+1;
				q.push(v);
				vis[v]=1;
			}
	}
}
main(){
	int u,v,deg;
	scanf("%d",&n);
	for (int i=1;i<=n;i++) dis[i]=-1;
	for (int i=0;i<n;i++){
		scanf("%d%d",&u,&deg);
		for (int j=0;j<deg;j++){
			scanf("%d",&v);
			G[u][v]=1;
		}
	}
	bfs(1);
	for (int i=1;i<=n;i++) printf("%d %d\n",i,dis[i]);
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 100+5
int num;
int m[MAX][MAX],a[MAX],d[MAX],color[MAX];//color[i]中0--白色；1--灰色；2--黑色； 
void bfs(int v){
	queue<int> q;
	q.push(v);d[v]=0;
	color[v]=1;
	int shou;
	while(!q.empty()){
	shou=q.front();q.pop();
	for(int i=1;i<=num;i++){
		if(m[shou][i]==1&&color[i]==0){
			q.push(i);d[i]=d[shou]+1;color[i]=1;
		}
		color[shou]=2;
	}
	}
	return ;
}
int main(){
	memset(m,0,sizeof(m));
	memset(color,0,sizeof(color));
	memset(d,-1,sizeof(d)); 
	cin>>num;
	for(int i=1;i<=num;i++){
		int cnt;
		cin>>a[i]>>cnt;
		for(int j=0;j<cnt;j++){
			int c;
			cin>>c;
			m[a[i]][c]=1; 
		}
	}
	bfs(1);
	for(int i=1;i<=num;i++){
		cout<<a[i]<<" "<<d[a[i]]<<endl;
	}
	
}