Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: //============================================================================
// Name        : Graph.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

struct Node{
	int id;
	vector<int> adjNodes;
	bool isFound;
	int discover_time;
	int finish_time;
	int distance;
};

class Graph{
public:
	vector<Node> nodeList;
	map<int, vector<Node*> > distanceMap;
	Graph():nodeList(), distanceMap(){}

	void insert(Node& i){
		nodeList.push_back(i);
	}

	void SearchDepthFirst(int source_id, int& timer){
		Node& source_node = nodeList.at(source_id);
		source_node.isFound = true;
		source_node.discover_time = timer;
		for(int i=0; i<source_node.adjNodes.size(); i++){
			if(nodeList.at(source_node.adjNodes.at(i)).isFound == false){
				timer ++;
				nodeList.at(source_node.adjNodes.at(i)).isFound = true;
				nodeList.at(source_node.adjNodes.at(i)).discover_time = timer;
				SearchDepthFirst(source_node.adjNodes.at(i), timer);
				nodeList.at(source_node.adjNodes.at(i)).finish_time = timer;
			}
		}
		timer++;

		source_node.finish_time = timer;
	}

	void SearchDepthFirstAll(int& timer){
		for(int i=0; i<nodeList.size(); i++){
			if(nodeList.at(i).isFound == false){
				SearchDepthFirst(i, timer);
				timer++;
			}
		}
	}

	void writeSearchResult(){
		for(int i=0; i<nodeList.size(); i++){
			cout << nodeList.at(i).id+1 << " " << nodeList.at(i).discover_time << " " << nodeList.at(i).finish_time << "\n";
		}
	}


	void addDistance(){
		vector<Node*> vec;
		vec.push_back(&nodeList.at(0));
		distanceMap[0] =vec;
		int cur_distance=0;

		while(cur_distance < nodeList.size()){
			vector<Node*>& vec_p_node = distanceMap[cur_distance];
			vector<Node*> empty_vec;
			distanceMap[cur_distance+1] = empty_vec;
			for(int i=0; i<vec_p_node.size(); i++){
				vector<int>& adjNodes = vec_p_node.at(i)->adjNodes;
				for(int j=0; j<adjNodes.size(); j++){
					if(nodeList.at(adjNodes.at(j)).distance == -1){
					    nodeList.at(adjNodes.at(j)).distance = cur_distance+1;
					    distanceMap[cur_distance+1].push_back(&nodeList.at(adjNodes.at(j)));
					}
				}
			}
			if(distanceMap[cur_distance+1].size() == 0){
				break;
			}

			cur_distance++;
		}
	}

	void writeDistance(){
		for(int i=0; i<nodeList.size(); i++){
			cout << i+1 << " " << nodeList.at(i).distance << "\n";
		}
	}

	void write(){
		for(int i=0; i<nodeList.size(); i++){
			Node& node = nodeList.at(i);
			cout << node.id << " " << node.adjNodes.size() << " ";

			for(int j=0; j<node.adjNodes.size(); j++){
				cout << node.adjNodes.at(j) << " ";
			}
			cout << "\n";
		}

	}

	void writeMap(){
		map<int, vector<Node*> >::iterator p;
		for(p=distanceMap.begin(); p!=distanceMap.end(); ++p){
			cout << "distance: " << p->first << ", id: ";
			vector<Node*>::iterator p2;
			for(p2=p->second.begin(); p2!=p->second.end(); ++p2){
				cout << (*p2)->id << " ";
			}
			cout << "\n";
		}
	}
};

void writeMatrix(vector<vector<int> > matrix){
	for(int i=0; i<matrix.size(); i++){
		for(int j=0; j<matrix.at(i).size()-1; j++){
			cout << matrix.at(i).at(j) << " ";
		}
		cout << matrix.at(i).at(matrix.at(i).size()-1);
		cout << "\n";
	}
}

int main() {
	int n;
	cin >> n;
	Graph g;

	for(int i=0; i<n; i++){
		int id, k;
		cin >> id >> k;
		vector<int> adjNodes;
		for(int j=0; j<k; j++){
			int input;
			cin >> input;
			adjNodes.push_back(input-1);
		}
		sort(adjNodes.begin(), adjNodes.end());
		Node node = {id-1, adjNodes, false, -1, -1, -1};
		g.insert(node);
	}

	//g.write();
	//int timer = 1;
	//g.SearchDepthFirstAll(timer);
	//g.writeSearchResult();

	g.nodeList.at(0).distance = 0;
	g.addDistance();
	g.writeDistance();

	return 0;
}

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

#include <iostream>
#include <queue>
using namespace std;
static const int INFTY = (1<<21);

int N;
int E[100][100];
int d[100];

void bfs(int s){
  queue<int> q;
  q.push(s);
  for (int j = 0; j < N; j++)d[j]=INFTY;
  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int v = 0; v < N; v++){
      if(E[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int k = 0; k < N; k++){
    if(d[k] == INFTY){
      cout << k+1 << " " << -1 << endl;
    }else{
      cout << k+1 << " " << d[k] << endl;
    }
  }
}



int main(){
  //Eへのグラフ情報の代入
  cin >> N;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      E[i][j] = 0;
    }
  }
  for ( int i = 0; i < N; i++){
    int dummy;
    cin >> dummy;
    int n;
    cin >> n;
    for (int c = 0; c < n; c++){
      int dest;
      cin >> dest;
      E[dummy-1][dest-1] = 1;
    }
  }

  bfs(0);

  return 0;


}