#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 2 * 1e18;
const long double PI = 3.14159265358979323846;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long fpow(long long a, long long b, long long m) {
  if (!b) return 1;
  long long ans = fpow(a * a % m, b / 2, m);
  return (b % 2 ? ans * a % m : ans);
}
long long inv(long long a, long long m) { return fpow(a, m - 2, m); }
pair<long long, long long> px[15], py[15];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, cnt1 = 0, cnt2 = 0;
  set<long long> ans1, ans2;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> px[i].first >> px[i].second;
  for (long long i = 0; i < m; i++) cin >> py[i].first >> py[i].second;
  for (long long i = 0; i < n; i++) {
    bool z = 0;
    set<long long> s;
    for (long long j = 0; j < m; j++) {
      if ((px[i].first == py[j].first && px[i].second == py[j].second) ||
          (px[i].first == py[j].second && px[i].second == py[j].first))
        continue;
      if (px[i].first == py[j].first) s.insert(px[i].first);
      if (px[i].first == py[j].second) s.insert(px[i].first);
      if (px[i].second == py[j].first) s.insert(px[i].second);
      if (px[i].second == py[j].second) s.insert(px[i].second);
    }
    if (s.size() == 1)
      ans1.insert(*s.begin());
    else if (s.size() > 1) {
      cnt1++;
    }
  }
  for (long long j = 0; j < m; j++) {
    bool z = 0;
    set<long long> s;
    for (long long i = 0; i < n; i++) {
      if ((px[i].first == py[j].first && px[i].second == py[j].second) ||
          (px[i].first == py[j].second && px[i].second == py[j].first))
        continue;
      if (px[i].first == py[j].first) s.insert(px[i].first);
      if (px[i].first == py[j].second) s.insert(px[i].first);
      if (px[i].second == py[j].first) s.insert(px[i].second);
      if (px[i].second == py[j].second) s.insert(px[i].second);
    }
    if (s.size() == 1)
      ans2.insert(*s.begin());
    else if (s.size() > 1) {
      cnt2++;
    }
  }
  if (cnt1 || cnt2)
    cout << -1;
  else if (ans2.size() == 1)
    cout << *ans2.begin();
  else if (ans1.size() == 1)
    cout << *ans1.begin();
  else
    cout << 0;
}
