#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> a[21], b[21];
set<pair<int, int> > ren1, ren2;
inline int check(pair<int, int> x, pair<int, int> y) {
  int a = x.first, b = x.second, c = y.first, d = y.second;
  if (a == c && b == d) return 0;
  if (a == c || a == d) return a;
  if (b == c || b == d) return b;
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 1; i <= m; ++i) scanf("%d%d", &b[i].first, &b[i].second);
  for (int i = 1; i <= n; ++i) {
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
    ren1.insert(a[i]);
  }
  for (int i = 1; i <= m; ++i) {
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
    ren2.insert(b[i]);
  }
  set<pair<int, int> >::iterator it1, it2;
  set<int> tmp1[20], tmp2[20];
  int cnt = 1;
  for (it1 = ren1.begin(); it1 != ren1.end(); it1++, cnt++) {
    int cnt1 = 1;
    for (it2 = ren2.begin(); it2 != ren2.end(); it2++, cnt1++) {
      int num = check(*it1, *it2);
      if (!num) continue;
      tmp1[cnt].insert(num);
      tmp2[cnt1].insert(num);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (tmp1[i].size() >= 2) return puts("-1"), 0;
  for (int i = 1; i <= m; ++i)
    if (tmp2[i].size() >= 2) return puts("-1"), 0;
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    if (tmp1[i].size() == 0) continue;
    if (ans == -1)
      ans = *tmp1[i].begin();
    else if (ans != *(tmp1[i].begin()))
      ans = 0;
  }
  for (int i = 1; i <= m; ++i) {
    if (tmp2[i].size() == 0) continue;
    if (ans == -1)
      ans = *tmp2[i].begin();
    else if (ans != *(tmp2[i].begin()))
      ans = 0;
  }
  printf("%d\n", ans);
}
