#include <bits/stdc++.h>
using namespace std;
long double PI = acosl(-1);
bool compare_int(int a, int b) { return (a > b); }
bool compare_string(string a, string b) { return a.size() < b.size(); }
bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.second == b.second)
    return a.first < b.first;
  else
    return (a.second > b.second);
}
bool cmp(pair<string, int> x, pair<string, int> y) {
  return (x.second < y.second);
}
void NA() {
  printf("NO\n");
  exit(0);
}
void YA() {
  printf("YES\n");
  exit(0);
}
const int N = 15;
vector<int> matchA[N];
vector<int> matchB[N];
int check(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first and a.second == b.second) return -1;
  if (a.first == b.second and a.second == b.first) return -1;
  if (a.first != b.first and a.first != b.second and a.second != b.first and
      a.second != b.second)
    return -1;
  if (a.first == b.first or a.first == b.second) return a.first;
  if (a.second == b.first or a.second == b.second) return a.second;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, x, y;
  vector<pair<int, int> > a, b;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a.push_back({x, y});
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    b.push_back({x, y});
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int token = check(a[i], b[j]);
      if (token == -1) continue;
      matchA[i].push_back(j);
      matchB[j].push_back(i);
      if (ans == -1)
        ans = token;
      else if (ans != token)
        ans = INT_MAX;
    }
  }
  if (ans == -1) {
    cout << -1 << endl;
    exit(0);
  }
  if (ans != INT_MAX) {
    cout << ans << endl;
    exit(0);
  }
  for (int i = 0; i < n; i++) {
    int token = -1;
    for (auto j : matchA[i]) {
      int temp = check(a[i], b[j]);
      if (token == -1)
        token = temp;
      else if (temp != token)
        token = INT_MAX;
    }
    if (token == INT_MAX) {
      cout << -1 << endl;
      exit(0);
    }
  }
  for (int i = 0; i < m; i++) {
    int token = -1;
    for (auto j : matchB[i]) {
      int temp = check(b[i], a[j]);
      if (token == -1)
        token = temp;
      else if (temp != token)
        token = INT_MAX;
    }
    if (token == INT_MAX) {
      cout << -1 << endl;
      exit(0);
    }
  }
  cout << 0 << endl;
}
