#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[20], b[20];
int cnt[20], cn2[20], val[20];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i].first >> b[i].second;
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i].first == b[j].first && a[i].second == b[j].second) continue;
      if (a[i].first == b[j].first || a[i].first == b[j].second)
        ++val[a[i].first], cnt[i] |= 1;
      if (a[i].second == b[j].first || a[i].second == b[j].second)
        ++val[a[i].second], cnt[i] |= 2;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i].first == b[j].first && a[i].second == b[j].second) continue;
      if (a[i].first == b[j].first || a[i].second == b[j].first) cn2[j] |= 1;
      if (a[i].first == b[j].second || a[i].second == b[j].second) cn2[j] |= 2;
    }
  }
  int cn = 0, r;
  for (int i = 1; i < 20; ++i)
    if (val[i]) ++cn, r = i;
  if (cn == 1)
    cout << r << '\n';
  else {
    for (int i = 0; i < n; ++i)
      if (cnt[i] == 3) return !(cout << -1 << '\n');
    for (int i = 0; i < m; ++i)
      if (cn2[i] == 3) return !(cout << -1 << '\n');
    cout << 0 << '\n';
  }
  return 0;
}
