#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e6 + 10;
const int N2 = 1e5 + 10;
const int off = 1e3;
const int inf = 1e9 + 10ll;
const int mod = 1e9 + 7;
const double eps = 1e-7;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
int n, m;
pair<int, int> a[20];
pair<int, int> b[20];
bool check(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p1.second || p2.first == p2.second) return false;
  int cnt = (int)(p1.first == p2.first) + (int)(p1.first == p2.second) +
            (int)(p1.second == p2.first) + (int)(p1.second == p2.second);
  return cnt == 1;
}
int geteq(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p2.first || p1.first == p2.second) return p1.first;
  return p1.second;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> 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> numd;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (check(a[i], b[j])) numd.insert(geteq(a[i], b[j]));
  if (numd.size() == 1) {
    cout << (*numd.begin()) << "\n";
    return 0;
  }
  bool ok = true;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (check(a[i], b[j])) {
        int d = geteq(a[i], b[j]);
        for (int z = 0; z < n; ++z)
          if (check(a[z], b[j]) && d != geteq(a[z], b[j])) ok = false;
        for (int z = 0; z < m; ++z)
          if (check(a[i], b[z]) && d != geteq(a[i], b[z])) ok = false;
      }
  if (ok)
    cout << 0 << "\n";
  else
    cout << -1 << "\n";
}
