#include <bits/stdc++.h>
using namespace std;
const int N = 120;
int n, m, a[N][2], b[N][2], buc[N], ans, cnt;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i][0] >> a[i][1];
  for (int i = 1; i <= m; i++) cin >> b[i][0] >> b[i][1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][0] == b[j][0] && a[i][1] != b[j][1]) buc[a[i][0]] = 1;
      if (a[i][0] == b[j][1] && a[i][1] != b[j][0]) buc[a[i][0]] = 1;
      if (a[i][1] == b[j][0] && a[i][0] != b[j][1]) buc[a[i][1]] = 1;
      if (a[i][1] == b[j][1] && a[i][0] != b[j][0]) buc[a[i][1]] = 1;
    }
  cnt = 0;
  for (int i = 1; i <= 9; i++)
    if (buc[i]) ans = i, cnt++;
  if (cnt == 1) {
    cout << ans << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    memset(buc, 0, sizeof(buc));
    for (int j = 1; j <= m; j++) {
      if (a[i][0] == b[j][0] && a[i][1] != b[j][1]) buc[a[i][0]] = 1;
      if (a[i][0] == b[j][1] && a[i][1] != b[j][0]) buc[a[i][0]] = 1;
      if (a[i][1] == b[j][0] && a[i][0] != b[j][1]) buc[a[i][1]] = 1;
      if (a[i][1] == b[j][1] && a[i][0] != b[j][0]) buc[a[i][1]] = 1;
    }
    cnt = 0;
    for (int i = 1; i <= 9; i++) cnt += (buc[i]);
    if (cnt >= 2) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int j = 1; j <= m; j++) {
    memset(buc, 0, sizeof(buc));
    for (int i = 1; i <= n; i++) {
      if (a[i][0] == b[j][0] && a[i][1] != b[j][1]) buc[a[i][0]] = 1;
      if (a[i][0] == b[j][1] && a[i][1] != b[j][0]) buc[a[i][0]] = 1;
      if (a[i][1] == b[j][0] && a[i][0] != b[j][1]) buc[a[i][1]] = 1;
      if (a[i][1] == b[j][1] && a[i][0] != b[j][0]) buc[a[i][1]] = 1;
    }
    cnt = 0;
    for (int i = 1; i <= 9; i++) cnt += (buc[i]);
    if (cnt >= 2) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
