#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > a(n), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  set<int> can;
  bool bad = 0;
  for (int i = 0; i < n; i++) {
    bool first = 0, second = 0;
    for (int j = 0; j < m; j++) {
      if (b[j] != a[i]) {
        if (a[i].first == b[j].first || a[i].first == b[j].second) first = 1;
        if (a[i].second == b[j].first || a[i].second == b[j].second) second = 1;
      }
    }
    if (first ^ second) {
      if (first) can.insert(a[i].first);
      if (second) can.insert(a[i].second);
    }
    bad |= first & second;
  }
  if (bad) {
    cout << -1;
    return 0;
  }
  if (can.size() == 1) {
    cout << *can.begin();
    return 0;
  }
  can.clear();
  for (int j = 0; j < m; j++) {
    bool first = 0, second = 0;
    for (int i = 0; i < n; i++) {
      if (b[j] != a[i]) {
        if (a[i].first == b[j].first || a[i].second == b[j].first) first = 1;
        if (a[i].first == b[j].second || a[i].second == b[j].second) second = 1;
      }
    }
    bad |= first & second;
  }
  if (bad) {
    cout << -1;
  } else {
    cout << 0;
  }
}
