#include <bits/stdc++.h>
using namespace std;
int a[20][2], b[20][2], n, m;
int geta(int i, int k) {
  int cnt = 0;
  for (int j = 1; j <= m; j++) {
    for (int p = 0; p <= 1; p++) {
      if (b[j][p] == a[i][k] && b[j][p ^ 1] != a[i][k ^ 1]) cnt++;
    }
  }
  return cnt;
}
int getb(int i, int k) {
  int cnt = 0;
  for (int j = 1; j <= n; j++) {
    for (int p = 0; p <= 1; p++) {
      if (a[j][p] == b[i][k] && a[j][p ^ 1] != b[i][k ^ 1]) cnt++;
    }
  }
  return cnt;
}
int Count[10];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i][0] >> a[i][1];
    if (a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i][0] >> b[i][1];
    if (b[i][0] > b[i][1]) swap(b[i][0], b[i][1]);
  }
  for (int i = 1; i <= n; i++) {
    Count[a[i][0]] += geta(i, 0);
    Count[a[i][1]] += geta(i, 1);
  }
  for (int i = 1; i <= m; i++) {
    Count[b[i][0]] += getb(i, 0);
    Count[b[i][1]] += getb(i, 1);
  }
  int cnt = 0;
  for (int i = 1; i <= 9; i++) cnt += (bool)Count[i];
  if (cnt == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= 1; j++) {
        if (geta(i, j)) {
          cout << a[i][j];
          return 0;
        }
      }
    }
  } else {
    bool flag = 1;
    for (int i = 1; i <= n; i++)
      if (geta(i, 0) > 0 && geta(i, 1) > 0) flag = 0;
    for (int i = 1; i <= m; i++) {
      if ((getb(i, 0) > 0 && getb(i, 1) > 0)) flag = 0;
    }
    if (flag)
      cout << 0;
    else
      cout << -1;
  }
  return 0;
}
