#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > ren1, ren2;
int n, m;
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 = 0; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ren1.insert(make_pair(min(u, v), max(u, v)));
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ren2.insert(make_pair(min(u, v), max(u, v)));
  }
  set<pair<int, int> >::iterator it1, it2;
  set<int> tmp1[20], tmp2[20];
  int cnt = 0;
  for (it1 = ren1.begin(); it1 != ren1.end(); it1++, cnt++) {
    int cnt1 = 0;
    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 = 0; i < n; i++)
    if (tmp1[i].size() >= 2) return 0 * puts("-1");
  for (int i = 0; i < m; i++)
    if (tmp2[i].size() >= 2) return 0 * puts("-1");
  int ans = -1;
  for (int i = 0; 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 = 0; 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);
  return 0;
}
