#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long int n, m;
  cin >> n >> m;
  long long int A[n][2];
  long long int B[m][2];
  for (long long int i = 0; i < n; ++i) {
    cin >> A[i][0] >> A[i][1];
  }
  for (long long int i = 0; i < m; ++i) {
    cin >> B[i][0] >> B[i][1];
  }
  long long int cnt = 0;
  long long int con = 0;
  long long int same = -1;
  for (long long int i = 0; i < n; ++i) {
    long long int cnt1 = -1, cnt2 = -1;
    long long int cnt3 = -1, cnt4 = -1;
    for (long long int j = 0; j < m; ++j) {
      if (A[i][0] == B[j][0] && A[i][1] != B[j][1]) {
        if (same != -1 && same != A[i][0]) {
          con = 1;
        }
        same = A[i][0];
        cnt1 = B[j][1];
      } else if (A[i][1] == B[j][1] && A[i][0] != B[j][0]) {
        if (same != -1 && same != A[i][1]) {
          con = 1;
        }
        same = A[i][1];
        cnt2 = B[j][0];
      } else if (A[i][1] == B[j][0] && A[i][0] != B[j][1]) {
        if (same != -1 && same != A[i][1]) {
          con = 1;
        }
        same = A[i][1];
        cnt2 = B[j][0];
      } else if (A[i][0] == B[j][1] && A[i][1] != B[j][0]) {
        if (same != -1 && same != A[i][0]) {
          con = 1;
        }
        same = A[i][0];
        cnt1 = B[j][0];
      }
    }
    if (cnt1 != -1 && cnt2 != -1 && A[i][0] != A[i][1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (long long int j = 0; j < m; ++j) {
    long long int cnt1 = -1, cnt2 = -1;
    for (long long int i = 0; i < n; ++i) {
      if (A[i][0] == B[j][0] && A[i][1] != B[j][1]) {
        if (same != -1 && same != A[i][0]) {
          con = 1;
        }
        same = A[i][0];
        cnt1 = A[i][1];
      } else if (A[i][1] == B[j][1] && A[i][0] != B[j][0]) {
        if (same != -1 && same != A[i][1]) {
          con = 1;
        }
        same = A[i][1];
        cnt2 = A[i][0];
      } else if (A[i][1] == B[j][0] && A[i][0] != B[j][1]) {
        if (same != -1 && same != A[i][1]) {
          con = 1;
        }
        same = A[i][1];
        cnt1 = B[j][0];
      } else if (A[i][0] == B[j][1] && A[i][1] != B[j][0]) {
        if (same != -1 && same != A[i][0]) {
          con = 1;
        }
        same = A[i][0];
        cnt2 = B[j][0];
      }
    }
    if (cnt1 != -1 && cnt2 != -1 && B[j][0] != B[j][1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (con == 1) {
    cout << 0 << endl;
  } else {
    cout << same << endl;
  }
  return 0;
}
