#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
int n, m;
pair<int, int> p[20], q[20];
int cnt[10], cnt2[10];
bool work(pair<int, int> a[], pair<int, int> b[], int n, int m) {
  for (int i = 0; i < n; i++) {
    bool flag1 = false, flag2 = false;
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) continue;
      if (b[j].first == a[i].first || b[j].second == a[i].first) flag2 = true;
      if (b[j].first == a[i].second || b[j].second == a[i].second) flag1 = true;
    }
    if (flag1 && flag2) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    pair<int, int> a;
    scanf("%d%d", &a.first, &a.second);
    if (a.first > a.second) swap(a.first, a.second);
    p[i] = a;
  }
  for (int i = 0; i < m; i++) {
    pair<int, int> a;
    scanf("%d%d", &a.first, &a.second);
    if (a.first > a.second) swap(a.first, a.second);
    q[i] = a;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (p[i] == q[j]) continue;
      if (p[i].first == q[j].first || p[i].first == q[j].second) {
        cnt[p[i].first]++;
      }
      if (p[i].second == q[j].first || p[i].second == q[j].second) {
        cnt[p[i].second]++;
      }
    }
  }
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      if (q[j] == p[i]) continue;
      if (q[j].first == p[i].first || q[j].first == p[i].second) {
        cnt2[q[j].first]++;
      }
      if (q[j].second == p[i].first || q[j].second == p[i].second) {
        cnt2[q[j].second]++;
      }
    }
  }
  int tot1 = 0, tot2 = 0, idx1 = -1, idx2 = -1;
  for (int i = 1; i <= 9; i++) {
    if (cnt[i]) tot1++, idx1 = i;
  }
  for (int i = 1; i <= 9; i++) {
    if (cnt2[i]) tot2++, idx2 = i;
  }
  if (tot1 == 1 && tot2 == 1 && idx1 == idx2) {
    printf("%d\n", idx1);
    return 0;
  } else if (tot1 == 0 || tot2 == 0) {
    puts("-1");
    return 0;
  }
  int tot = 0;
  if (work(p, q, n, m)) tot++;
  if (work(q, p, m, n)) tot++;
  if (tot == 2) {
    puts("0");
  } else {
    puts("-1");
  }
  return 0;
}
