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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 128;

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

void solve(void){
	ENJYU;
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	memset(d, -1, sizeof(d));
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] > 0) continue;
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

C++: //Breadth First Search
#include <iostream>
#include <queue>

using namespace std;

static const int N = 100;

int n, M[N][N];
int d[N];
queue<int> Q;

void bfs() {
    int u;
    for(int i = 0; i < N; i++) d[i] = -1;
    Q.push(0);
    d[0] = 0;
    while(!Q.empty()) {
        u = Q.front();
        for(int i = 0; i < n; i++) {
            if(M[u][i] == 1 && d[i] == -1) {
                Q.push(i);
                d[i] = d[u] + 1;
            }
        }
        Q.pop();
    }
    for(int i = 0; i < n; i++) {
        cout << i + 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;
        for(int j = 0; j < k; j++) {
            cin >> v;
            M[u - 1][v - 1] = 1;
        }
    }

    bfs();
}

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

int n,m[101][101];
int d[101],t=0,vis[101];
queue<int> q;

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

int main(){
    int i,j,k,u,v,du;
    scanf("%d",&n);
    k = n;
    while(k--){
        scanf("%d",&u);
        scanf("%d",&du);
        while(du--){
            scanf("%d",&v);
            m[u][v]  = 1;
        }
    }
    bfs();
}

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

int G[110][110] = {0}; /*G????????????????????????????????????????????????, ??????1?????¨??????????????????*/

int n;
int D[110]; /*D???????????????????¨?????????°????????????????????????????????????*/
void bfs(int src){
  
  // cerr << "bfs root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while(! Q.empty()) {
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting " << cur << ' ' << D[cur] << endl;
    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++){
    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);
}

C++: #include<algorithm>
#include<bitset>
#include<iostream>
#include<iomanip>
#include<set>
#include<string>
#include<math.h>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;

class zahyo{
	public:
	int x;
	int y;
	zahyo(){
		x=0;
		y=0;
	}
	zahyo(int x_,int y_){
		x=x_;
		y=y_;
	}
	void print(){
		cout<<x<<" "<<y;
	}
	zahyo operator+(zahyo add){
		add.x=this->x+add.x;
		add.y=this->y+add.y;
		return add;
	}
	zahyo operator-(zahyo add){
		add.x=this->x-add.x;
		add.y=this->y-add.y;
		return add;
	}
	bool operator==(const zahyo add) const{
		if(add.x==this->x and add.y==this->y){return true;}
		else{return false;}
	}
	bool operator!=(const zahyo add) const{
		if(add.x==this->x or add.y==this->y){return false;}
		else{return true;}
	}
	bool operator<(const zahyo add) const{
		if(this->x<add.x){return true;}
		else if(this->x==add.x and this->y<add.y){return true;}
		else{return false;}
	}
	bool operator>(const zahyo add) const{
		if(this->x>add.x){return true;}
		else if(this->x==add.x and this->y>add.y){return true;}
		else{return false;}
	}
	void input(){
		cin>>x>>y;
	}
};


int main(){
	int glaph[100][100]={};
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int k;
		cin>>k>>k;
		for(int j=0;j<k;j++){
			int v;
			cin>>v;
			v--;
			glaph[i][v]=1;
		}
	}
	queue<int> widesearch;
	int ans[100]={};
	int counter=1;
	ans[0]=1;
	widesearch.push(0);
	while(widesearch.size()>0){
		counter++;
		int size=widesearch.size();
		for(int i=0;i<size;i++){
			int from=widesearch.front();
			widesearch.pop();
			for(int j=0;j<n;j++){
				if(glaph[from][j]==1 and ans[j]==0){
					ans[j]=counter;
					widesearch.push(j);
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<ans[i]-1<<endl;
	}
	return 0;
}