
#include <array>
#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

using state = array<int, 9>;

bool
state_ok(state curr)
{
	for (size_t i = 0; i < 9; i++) {
		if (curr[i] != (i == 8 ? 0 : i + 1)) {
			return false;
		}
	}
	return true;
}

int
main2()
{
	queue<state> open;
	set<state> visited;
	map<state, state> previous_state;

	state s;
	for (size_t i = 0; i < 9; i++)
		cin >> s[i];
	open.push(s);

	while (!open.empty()) {
		state curr = open.front();
		open.pop();

		if (state_ok(curr)) {
			cout << "Vse je na svem miste" << endl;
			for (state i = curr; previous_state.count(i);
			     i = previous_state[i]) {
				for (auto ii : i)
					cout << ii << ' ';
				cout << endl;
				if (i == s)
					break;
			}

			return 0;
		}

		if (visited.count(curr))
			continue;
		visited.insert(curr);

		int nullpos = 0;
		for (size_t i = 0; i < curr.size(); i++) {
			if (curr[i] == 0) {
				nullpos = i;
				break;
			}
		}

		// posun kachlicky dolu, nula se posouva nahoru
		if (nullpos > 2) {
			state newstate = curr;
			swap(newstate[nullpos - 3], newstate[nullpos]);
			if (!previous_state.count(newstate))
				previous_state[newstate] = curr;
			open.push(newstate);
		}

		// posun kachlicky nahoru, nula se posouva dolu
		if (nullpos < 6) {
			state newstate = curr;
			swap(newstate[nullpos + 3], newstate[nullpos]);
			if (!previous_state.count(newstate))
				previous_state[newstate] = curr;
			open.push(newstate);
		}

		// posun kachlicky doprava, nula se posouva doleva
		if (nullpos % 3 != 0) {
			state newstate = curr;
			swap(newstate[nullpos], newstate[nullpos - 1]);
			if (!previous_state.count(newstate))
				previous_state[newstate] = curr;
			open.push(newstate);
		}

		// posun kachlicky doleva, nula se posouva doprava
		if (nullpos % 3 != 2) {
			state newstate = curr;
			swap(newstate[nullpos], newstate[nullpos + 1]);
			if (!previous_state.count(newstate))
				previous_state[newstate] = curr;
			open.push(newstate);
		}
	}

	cout << "nein" << endl;

	return 0;
}

int
main()
{
	map<int, int> h;
	for (;;) {
		int a, b;

		cin >> a;
		if (!a)
			break;
		cin >> b;

		if (h.count(a)) {
			a = h[a];
		} else {
			h[a] = a;
		}
		if (h.count(b)) {
			b = h[b];
		} else {
			h[b] = b;
		}
		for (auto &i : h) {
			if (i.second == b)
				i.second = a;
		}
	}

	set<int> count;
	for_each(
	  h.begin(), h.end(), [&count](auto par) { count.insert(par.second); });

	cout << endl << count.size() << endl;
	return 0;
}
