#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[111], b[111];
set<int> as[22], bs[22];
int n, m;
void read(pair<int, int> &x) { scanf("%d%d", &x.first, &x.second); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) read(a[i]);
  for (int i = 1; i <= n; ++i) {
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
    as[a[i].first].insert(i), as[a[i].second].insert(i);
  }
  for (int i = 1; i <= m; ++i) {
    read(b[i]);
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
    bs[b[i].first].insert(i), bs[b[i].second].insert(i);
  }
  bool myok = true, ok1 = true, ok2 = true;
  int okcnt = 0, ans = -1;
  for (int i = 1; i <= n; ++i) {
    map<int, int> res;
    int cur = -1;
    for (auto g : bs[a[i].first]) res[g] = a[i].first;
    for (auto g : bs[a[i].second]) res[g] = a[i].second;
    for (int j = 1; j <= m; ++j)
      if (b[j] == a[i]) res.erase(j);
    for (auto g : res) {
      if (cur == -1)
        cur = g.second;
      else if (cur != g.second)
        ok1 = false, okcnt += 5;
    }
    if (cur != -1 && res.begin()->second != ans) okcnt += 1;
    if (res.size() > 0) ans = res.begin()->second;
  }
  int ans3 = 0;
  ans = -1;
  for (int i = 1; i <= m; ++i) {
    map<int, int> res;
    int cur = -1;
    for (auto g : as[b[i].first]) res[g] = b[i].first;
    for (auto g : as[b[i].second]) res[g] = b[i].second;
    for (int j = 1; j <= n; ++j)
      if (a[j] == b[i]) res.erase(j);
    for (auto g : res) {
      if (cur == -1)
        cur = g.second;
      else if (cur != g.second)
        ok2 = false, ans3 += 5;
    }
    if (cur != -1 && res.begin()->second != ans) ans3 += 1;
    if (res.size() > 0) ans = res.begin()->second;
  }
  okcnt = min(okcnt, ans3);
  if (okcnt == 1) {
    printf("%d\n", ans);
  } else if (okcnt > 1) {
    if (ok1 && ok2) {
      puts("0");
    } else
      puts("-1");
  } else
    assert(false);
  return 0;
}
