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<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n;
vector<int> a[100];
int dist[100];
const int INF = 100000000;

int main() {
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    rep(i, y) {
      int z;
      cin >> z;
      a[x-1].push_back(z-1);
    }
  }

    rep(i, n) {
      dist[i] = INF;
    }
    dist[0] = 0;

    queue<int> que;

    que.push(0);
    while(que.size()) {
      int p = que.front();
      que.pop();

      for(int i = 0; i < a[p].size(); i++) {
        if(dist[a[p][i]] > dist[p] + 1) {
          dist[a[p][i]] = dist[p] + 1;
          que.push(a[p][i]);
        }
      }
    }

  rep(i, n) {
    if(dist[i] != INF) cout << i + 1 << " " << dist[i] << endl;
    else cout << i + 1 << " " << -1 << endl;
  }
    

  return 0;
}

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

static const int N = 100;
static const int INF = (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] = INF;
	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] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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--; // 0オリジン
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	
	return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int main() {
    int V, k, source, target, current, next;
    bool oneVisited;
    scanf("%d", &V);
    vector<int> G[V+1];
    bool visited[V+1];
    int disc[V+1];
    fill(visited, visited+V+1, 0);
    fill(disc, disc+V+1, -1);
    for (int i = 0; i < V; i++) {
        scanf("%d %d", &source, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &target);
            G[source].push_back(target);
        }
    }
    queue<int> q;
    q.push(1);
    int d = 0; disc[1] = 0;
    visited[1] = true;
    while (!q.empty()) {
        current = q.front(); q.pop();
        for (int i = 0; i < G[current].size(); i++) {
            next = G[current][i];
            if (!visited[next]) {
                visited[next] = true;
                disc[next] = disc[current]+1;
                q.push(next);
            }
        }
    }
    for (int i = 1; i <= V; i++) printf("%d %d\n", i, disc[i]);
    return 0;
}

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

#define M 1000000007
vector<int> dist;
vector<vector<int>> edge;

int main() {
    int n;
    cin >> n;
    dist.resize(n, M);
    edge.resize(n);
    for (int i = 0; i < n; ++i) {
        int id, k;
        cin >> id >> k;
        if (id != i + 1) exit(1);
        if (!k) continue;

        edge[i].resize(k);
        for (int j = 0; j < k; ++j) {
            cin >> edge[i][j];
            --edge[i][j];
        }
    }
    queue<int> q;
    q.push(0);
    dist[0] = 0;
    while (!q.empty()) {
        int p = q.front();
        q.pop();
        for (int t : edge[p]) {
            if (dist[t] > dist[p] + 1) {
                dist[t] = dist[p] + 1;
                q.push(t);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << (i + 1) << " " << (dist[i] < M ? dist[i] : -1) << "\n";
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
/*
class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};
*/
int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200];

int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }

   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j; 
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }

  //Queue que = Queue();
  queue<int> que;
  que.push(u[0]);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
 
  for(int i=0;i<n;i++){
    cout << u[i] << " " << ans[u[i]] << endl;
  }

  return 0;
}