#include <bits/stdc++.h>
using namespace std;
void func(void) {
  freopen("input.c", "r", stdin);
  freopen("output.c", "w", stdout);
}
vector<pair<long long, long long> > v1, v2;
int main() {
  long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m,
                     fg = 0, mx = 0, mx1 = 0, mn = 8000000000000000000,
                     mn1 = 8000000000000000000;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (x > y) swap(x, y);
    v1.push_back({x, y});
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (x > y) swap(x, y);
    v2.push_back({x, y});
  }
  set<long long> last;
  for (int i = 0; i < n; i++) {
    pair<long long, long long> p1 = v1[i];
    set<long long> s;
    s.clear();
    for (int j = 0; j < m; j++) {
      pair<long long, long long> p2 = v2[j];
      if (v1[i] == v2[j]) continue;
      if (v1[i].first == v2[j].first) {
        s.insert(v1[i].first);
      }
      if (v1[i].first == v2[j].second) {
        s.insert(v1[i].first);
      }
      if (v1[i].second == v2[j].first) {
        s.insert(v1[i].second);
      }
      if (v1[i].second == v2[j].second) {
        s.insert(v1[i].second);
      }
    }
    if (s.size() >= 2) {
      cout << -1 << endl;
      return 0;
    }
    if (s.size() == 0) continue;
    auto it = s.begin();
    last.insert(*it);
  }
  for (int i = 0; i < m; i++) {
    pair<long long, long long> p1 = v2[i];
    set<long long> s;
    s.clear();
    for (int j = 0; j < n; j++) {
      pair<long long, long long> p2 = v1[j];
      if (v2[i] == v1[j]) continue;
      if (v2[i].first == v1[j].first) {
        s.insert(v2[i].first);
      }
      if (v2[i].first == v1[j].second) {
        s.insert(v2[i].first);
      }
      if (v2[i].second == v1[j].first) {
        s.insert(v2[i].second);
      }
      if (v2[i].second == v1[j].second) {
        s.insert(v2[i].second);
      }
    }
    if (s.size() >= 2) {
      cout << -1 << endl;
      return 0;
    }
    if (s.size() == 0) continue;
    auto it = s.begin();
    last.insert(*it);
  }
  if (last.size() >= 2) {
    cout << 0 << endl;
    return 0;
  }
  cout << *last.begin() << endl;
}
