#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long b, long long p = MOD) {
  a = a % p;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const long long N = 2 * 1e5 + 2;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > a, b;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    if (y < x) swap(x, y);
    a.push_back({x, y});
  }
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    if (y < x) swap(x, y);
    b.push_back({x, y});
  }
  set<long long> ans;
  for (long long i = 0; i < n; i++) {
    set<long long> o;
    for (long long j = 0; j < m; j++) {
      long long cnt = 0;
      if (a[i].first == b[j].first) cnt++;
      if (a[i].first == b[j].second) cnt++;
      if (a[i].second == b[j].first) cnt++;
      if (a[i].second == b[j].second) cnt++;
      if (cnt == 1) {
        if (a[i].first == b[j].first)
          o.insert(a[i].first), ans.insert(a[i].first);
        if (a[i].first == b[j].second)
          o.insert(a[i].first), ans.insert(a[i].first);
        if (a[i].second == b[j].first)
          o.insert(a[i].second), ans.insert(a[i].second);
        if (a[i].second == b[j].second)
          o.insert(a[i].second), ans.insert(a[i].second);
      }
    }
    if (o.size() > 1) {
      cout << "-1";
      return 0;
    }
  }
  for (long long j = 0; j < m; j++) {
    set<long long> o;
    for (long long i = 0; i < n; i++) {
      long long cnt = 0;
      if (a[i].first == b[j].first) cnt++;
      if (a[i].first == b[j].second) cnt++;
      if (a[i].second == b[j].first) cnt++;
      if (a[i].second == b[j].second) cnt++;
      if (cnt == 1) {
        if (a[i].first == b[j].first)
          o.insert(a[i].first), ans.insert(a[i].first);
        if (a[i].first == b[j].second)
          o.insert(a[i].first), ans.insert(a[i].first);
        if (a[i].second == b[j].first)
          o.insert(a[i].second), ans.insert(a[i].second);
        if (a[i].second == b[j].second)
          o.insert(a[i].second), ans.insert(a[i].second);
      }
    }
    if (o.size() > 1) {
      cout << "-1";
      return 0;
    }
  }
  if (ans.size() == 1) {
    cout << (*ans.begin());
  } else if (ans.size() > 1) {
    cout << 0;
  } else
    cout << "-1";
}
