Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

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

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

Int N;
vector<vector<Int> > G(101);
vector<Int> D(101, -1);

void input() {
  cin >> N;
  Int u, c, v;
  rep(N) {
    cin >> u >> c;u--;
    rep(c) {
      cin >> v;v--;
      G.at(u).push_back(v);
    }
  }
}

void bfs() {
  queue<Int> Q;
  Q.push(0);
  D.at(0) = 0;
  while (Q.size()) {
    Int u = Q.front();Q.pop();
    for (auto v: G.at(u)) {
      if (D.at(v) != -1) continue;
      Q.push(v);
      D.at(v) = D.at(u) + 1;
    }
  }
}

void solve() {
  bfs();
  for (int i=0; i<N; i++) {
    cout << i+1 << " " << D.at(i) << endl;
  }
}

int main() {
  input();
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int n;
vector<vector<int> >g;
int dist[110];
void bfs(int u) {
    queue<int> q;
    q.push(u);
    rep(i,n) dist[i] = -1;
    dist[u] = 0;
    while(!q.empty()) {
        int v = q.front();q.pop();
        for(int i = 0;i<g[v].size();i++) {
            int nv = g[v][i];
            if(dist[nv] == -1) {
                q.push(nv);
                dist[nv] = dist[v] + 1;
            }               
        }
    }
}
int main() {
    cin >> n;
    g.assign(n,vector<int>());
    rep(i,n) {
        int u,k;
        cin >> u >> k;
        u--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n) cout << i+1 << " " << dist[i] << endl;
}

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

using namespace std;

struct Node{
  int p[100]={0}, d=-1, c[100], c_n;
};

Node nodes[101];

void search(int x){
    for(int i=0;i<nodes[x].c_n;++i){
        if(nodes[nodes[x].c[i]].d > nodes[x].d + 1 || nodes[nodes[x].c[i]].d == -1){
            nodes[nodes[x].c[i]].d = nodes[x].d + 1;
        }
    }
    
    for(int i=0;i<nodes[x].c_n;++i){
        if(nodes[nodes[x].c[i]].d == nodes[x].d + 1){
            search(nodes[x].c[i]);
        }
    }
    return;
}

int main(){
    int n, num, child;
    cin >> n;
    
    
    
    for(int i=1;i<=n;++i){
        cin >> num;
        
        if(num == 1){
            nodes[num].d=0;
        }
        
        cin >> nodes[num].c_n;
        for(int j=0;j<nodes[num].c_n;++j){
            cin >> nodes[num].c[j];
        }
    }
    
    search(1);
    
    for(int i=1;i<=n;++i){
        cout << i << " " << nodes[i].d << endl;
    }
    
    
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B

#include <iostream>
#include <iomanip>

#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <queue>

typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)

using namespace std;

// change min/max
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> void output(std::vector<T> & A) {
    const int n = A.size();
    for (int i = 0; i < n - 1; ++i) {
        std::cout << A[i] << " ";
    }
    std::cout << A[n - 1] << std::endl;
}

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
vector<vector<int>> M;
vector<int> colour, d;
int tt = 0;
queue<int> que;

void bfs(int u) {
    que.push(u);
    colour[u] = GRAY;

    while (!que.empty()) {
        const int v = que.front(); que.pop();
        for (int s : M[v]) {
            if (colour[s] == WHITE) {
                que.push(s);
                d[s] = d[v] + 1;
                colour[s] = GRAY;
            }
        }
        colour[v] = BLACK;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout << std::fixed << std::setprecision(15);

    // populate M
    int n; cin >> n;
    M = vector<vector<int>>(n);
    colour = vector<int>(n, 0);
    d = vector<int>(n, -1);

    rep(i, n) {
        int from; cin >> from; --from;
        int m; cin >> m;
        rep(j, m) {
            int to; cin >> to; --to;
            M[from].push_back(to);
            // M[to].push_back(from);
        }
    }

    // bfs
    d[0] = 0;
    bfs(0);

    rep(i, n) {
        cout << (i + 1) << " " << d[i] << endl;
    }

    return 0;
}