#include <bits/stdc++.h>
using namespace std;
int getcom(pair<int, int> a, pair<int, int> b) {
  vector<int> all;
  all.push_back(a.first);
  all.push_back(a.second);
  all.push_back(b.first);
  all.push_back(b.second);
  sort(all.begin(), all.end());
  int com = -1;
  for (int k = 1; k < 4; k++) {
    if (all[k] == all[k - 1]) {
      com = all[k];
    }
  }
  return com;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > a;
  vector<pair<int, int> > b;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    a.push_back(make_pair(x, y));
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    b.push_back(make_pair(x, y));
  }
  vector<int> possible;
  bool theyknow = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        continue;
      }
      int com = getcom(a[i], b[j]);
      if (com == -1) {
        continue;
      }
      if (com != -1) {
        possible.push_back(com);
      }
      for (int k = 0; k < m; k++) {
        if (a[i] == b[k]) {
          continue;
        }
        int com2 = getcom(a[i], b[k]);
        if (com2 != -1 && com2 != com) {
          theyknow = false;
        }
      }
      for (int k = 0; k < n; k++) {
        if (a[k] == b[j]) {
          continue;
        }
        int com2 = getcom(a[k], b[j]);
        if (com2 != -1 && com2 != com) {
          theyknow = false;
        }
      }
    }
  }
  sort(possible.begin(), possible.end());
  if (possible[0] == possible[possible.size() - 1]) {
    cout << possible[0] << endl;
    return 0;
  }
  if (theyknow) {
    cout << 0 << endl;
  } else {
    cout << -1 << endl;
  }
}
