process.stdin.resume();
process.stdin.setEncoding("utf8");
let input = "";
process.stdin.on("data", function(chunk) {input += chunk;});
process.stdin.on("end", function() {main(input.split("\n"));});

const NIL = -1;

class Queue {
	constructor(size) {
		this.top = 0;
		this.tail = 0;
		this.ary = new Array(size);
	}
	isEmpty() {
		return this.top === this.tail;
	}
	enqueue(val) {
		this.ary[this.tail++] = val;
	}
	dequeue() {
		return this.ary[this.top++];
	}
}

function allocNode(u,k) {
	const p = {};
	p.value = u;
	p.depth = NIL;
	p.chldNum = k;
	p.chldHead = k > 0 ? new Array(k) : null;
	return p;
}

function bfs(q,top) {
	let i, u, v;
	q.enqueue(0);
	top[0].depth = 0;
	while (!q.isEmpty()) {
		u = q.dequeue();
		for (i = 0; i < top[u].chldNum; i++) {
			v = top[u].chldHead[i];
			if (top[v].depth != NIL) continue;
			top[v].depth = top[u].depth + 1;
			q.enqueue(v);
		}
	}
}

function main(lines) {
	let i, j, u, k
	let line;
	const n = Number(lines[0]);

	const q = new Queue(n);

	const top = new Array(n);
	for (i = 0; i < n; i++) {
		line = lines[1 + i].split(" ").map(Number);
		u = line[0];
		k = line[1];
		top[i] = allocNode(u,k);
		for (j = 0; j < k; j++)	top[i].chldHead[j] = line[2 + j] - 1;
	}

	bfs(q,top);

	for (i = 0; i < n; i++) console.log(top[i].value,top[i].depth);
}
