#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  long long n, m, a[20][20], b[20][20];
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    if (x < y) {
      a[i][0] = x;
      a[i][1] = y;
    } else {
      a[i][0] = y;
      a[i][1] = x;
    }
  }
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    if (x < y) {
      b[i][0] = x;
      b[i][1] = y;
    } else {
      b[i][0] = y;
      b[i][1] = x;
    }
  }
  long long cnt = 0, check = 0;
  long long ans1[10000], ans2[10000], ans3[10000], ans4[10000], ans5[10000];
  for (long long p = 1; p < 10; p++) {
    for (long long q = p + 1; q < 10; q++) {
      for (long long r = 1; r < 10; r++) {
        for (long long s = r + 1; s < 10; s++) {
          if (p == r && q == s) {
            continue;
          }
          if (p != r) {
            if (p != s) {
              if (q != r) {
                if (q != s) continue;
              }
            }
          }
          for (long long i = 0; i < n; i++) {
            if (p == a[i][0] && q == a[i][1]) {
              check++;
              break;
            }
          }
          if (check == 0) {
            continue;
          }
          check = 0;
          for (long long i = 0; i < m; i++) {
            if (r == b[i][0] && s == b[i][1]) {
              check++;
              break;
            }
          }
          if (check == 0) {
            continue;
          }
          check = 0;
          ans1[cnt] = p;
          ans2[cnt] = q;
          ans3[cnt] = r;
          ans4[cnt] = s;
          if (p == r || p == s) {
            ans5[cnt] = p;
          } else if (q == r || q == s) {
            ans5[cnt] = q;
          }
          cnt++;
        }
      }
    }
  }
  check = 0;
  for (long long i = 0; i < cnt - 1; i++) {
    if (ans5[i] != ans5[i + 1]) {
      check++;
      break;
    }
  }
  if (check == 0) {
    cout << ans5[0] << '\n';
  } else {
    check = 0;
    for (long long i = 0; i < cnt; i++) {
      for (long long j = i + 1; j < cnt; j++) {
        if (ans1[i] == ans1[j] && ans2[i] == ans2[j]) {
          if (ans5[i] != ans5[j]) {
            check++;
          }
        }
        if (ans3[i] == ans3[j] && ans4[i] == ans4[j]) {
          if (ans5[i] != ans5[j]) {
            check++;
          }
        }
      }
    }
    if (check > 0) {
      cout << -1 << '\n';
    } else {
      cout << 0 << '\n';
    }
  }
}
