#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
  }
  set<int> s;
  for (int i = 0; i < n; i++) {
    int f = 0;
    for (int j = 0; j < m; j++) {
      if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&
          ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {
        continue;
      } else if ((a[i].first == b[j].first) || (a[i].first == b[j].second)) {
        f = 1;
      }
    }
    int tr = 1;
    if (f) {
      s.insert(a[i].first);
      tr = 0;
    }
    for (int j = 0; j < m; j++) {
      if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&
          ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {
        continue;
      } else if ((a[i].second == b[j].first) || (a[i].second == b[j].second)) {
        f += 1;
        break;
      }
    }
    if (f > 1) {
      cout << "-1\n";
      exit(0);
    }
    if (f && tr) {
      s.insert(a[i].second);
    }
  }
  for (int j = 0; j < m; j++) {
    int f = 0;
    int tr = 1;
    for (int i = 0; i < n; i++) {
      if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&
          ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {
        continue;
      } else if ((b[j].first == a[i].first) || (b[j].first == a[i].second)) {
        f = 1;
      }
    }
    if (f) {
      s.insert(b[j].first);
      tr = 0;
    }
    for (int i = 0; i < n; i++) {
      if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&
          ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {
        continue;
      } else if ((b[j].second == a[i].first) || (b[j].second == a[i].second)) {
        f += 1;
        break;
      }
    }
    if (f > 1) {
      cout << "-1\n";
      exit(0);
    }
    if (f && tr) {
      s.insert(b[j].second);
    }
  }
  if (s.size() == 1) {
    auto it = s.begin();
    cout << (*it);
  } else {
    cout << "0";
  }
  return 0;
}
