#include <bits/stdc++.h>
using namespace std;
const int maxn = 350;
const int inf = 0x3f3f3f3f;
pair<int, int> a[50], b[50];
bool is[12];
void mch(pair<int, int> l, pair<int, int> r) {
  if (l.first == r.first and l.second == r.second) return;
  if (l.first == r.first)
    is[l.first] = true;
  else if (l.first == r.second)
    is[l.first] = true;
  if (l.second == r.first)
    is[l.second] = true;
  else if (l.second == r.second)
    is[l.second] = true;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; 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 = 1; i <= m; ++i) {
    cin >> b[i].first >> b[i].second;
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  bool i_know = false;
  memset(is, 0, sizeof is);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) mch(a[i], b[j]);
  int cnt = 0;
  for (int i = 1; i <= 9; ++i)
    if (is[i]) ++cnt;
  if (cnt == 1) i_know = true;
  if (i_know) {
    for (int i = 1; i <= 9; ++i)
      if (is[i]) {
        cout << i;
        return 0;
      }
  }
  bool a_know = true, b_know = true;
  for (int i = 1; i <= n; ++i) {
    cnt = 0;
    memset(is, 0, sizeof is);
    for (int j = 1; j <= m; ++j) mch(a[i], b[j]);
    for (int j = 1; j <= 9; ++j)
      if (is[j]) ++cnt;
    if (cnt > 1) a_know = false;
  }
  for (int j = 1; j <= m; ++j) {
    cnt = 0;
    memset(is, 0, sizeof is);
    for (int i = 1; i <= n; ++i) mch(a[i], b[j]);
    for (int i = 1; i <= 9; ++i)
      if (is[i]) ++cnt;
    if (cnt > 1) b_know = false;
  }
  if (a_know and b_know)
    cout << 0;
  else
    cout << -1;
  return 0;
}
