#include <bits/stdc++.h>
using namespace std;
int main() {
  int kazu, takahasi, aoki;
  cin >> kazu >> takahasi >> aoki;
  vector<vector<int>> henn(kazu, vector<int>(2));
  queue<int> que;
  que.push(takahasi);
  for (int i = 0; i <= kazu - 1; i++) {
    for (int j = 0; j <= 1; j++) {
      cin >> henn[i][j];
    }
  }
  int now = 0;
  int kaisuu = 0;
  int tyouhuku = 0;
  int saitann = 0;
  for (int i = 0; i <= kazu - 1; i++) {
    for (int j = 0; j <= 1; j++) {
      if (que.front() == aoki) {
        saitann = kaisuu - tyouhuku;
        i = kazu;
        j = kazu;
      }
      now = que.front();
      que.pop();
      if (henn[i][j] == now) {
        que.push(henn[i][0 - j + 1]);
        tyouhuku++;
      }
      tyouhuku--;
      kaisuu++;
    }
  }
}
