#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int n, m;
pair<int, int> a[N], b[N];
vector<int> match[N], match2[N];
int common(int i, int j) {
  if (a[i].first == b[j].first or a[i].first == b[j].second) return a[i].first;
  if (a[i].second == b[j].first or a[i].second == b[j].second)
    return a[i].second;
  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);
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &b[i].first, &b[i].second);
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i] != b[j] and common(i, j))
        match[i].push_back(j), match2[j].push_back(i);
  set<int> st;
  for (int i = 1; i <= n; ++i)
    for (int j : match[i]) st.insert(common(i, j));
  if (((int)(st).size()) == 1) return cout << *st.begin(), 0;
  bool ok = true;
  for (int i = 1; i <= n; ++i) {
    int x = 0, y = 0;
    for (int j : match[i]) {
      if (common(i, j) == a[i].first)
        ++x;
      else
        ++y;
    }
    ok &= min(x, y) == 0;
  }
  for (int j = 1; j <= m; ++j) {
    int x = 0, y = 0;
    for (int i : match2[j]) {
      if (common(i, j) == b[j].first)
        ++x;
      else
        ++y;
    }
    ok &= min(x, y) == 0;
  }
  if (ok)
    cout << 0;
  else
    cout << -1;
}
