#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n1, n2, a[maxn][2], b[maxn][2];
int visa[maxn][2], visb[maxn][2];
int main() {
  scanf("%d%d", &n1, &n2);
  for (int i = 1; i <= n1; i++) {
    scanf("%d%d", &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 <= n2; i++) {
    scanf("%d%d", &b[i][0], &b[i][1]);
    if (b[i][0] > b[i][1]) swap(b[i][0], b[i][1]);
  }
  int cnt = 0, res = 0;
  for (int i = 1; i <= n1; i++)
    for (int j = 1; j <= n2; j++) {
      if (a[i][0] == b[j][0] && a[i][1] == b[j][1]) continue;
      for (int t1 = 0; t1 <= 1; t1++)
        for (int t2 = 0; t2 <= 1; t2++)
          if (a[i][t1] == b[j][t2]) {
            if (a[i][t1] != res) cnt++;
            res = a[i][t1];
            visa[i][t1]++, visb[j][t2]++;
          }
    }
  if (cnt == 1) return printf("%d\n", res), 0;
  for (int i = 1; i <= n1; i++)
    if (visa[i][0] && visa[i][1]) return puts("-1"), 0;
  for (int i = 1; i <= n2; i++)
    if (visb[i][0] && visb[i][1]) return puts("-1"), 0;
  return puts("0"), 0;
  return 0;
}
