#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr1[15][2];
int arr2[15][2];
int cnt1[15];
int cnt2[15];
int num1[15];
int num2[15];
int ans = 0;
int connect(int a, int b) {
  if (arr1[a][0] == arr2[b][0] && arr1[a][1] != arr2[b][1]) {
    return arr1[a][0];
  }
  if (arr1[a][0] == arr2[b][1] && arr1[a][1] != arr2[b][0]) {
    return arr1[a][0];
  }
  if (arr1[a][1] == arr2[b][0] && arr1[a][0] != arr2[b][1]) {
    return arr1[a][1];
  }
  if (arr1[a][1] == arr2[b][1] && arr1[a][0] != arr2[b][0]) {
    return arr1[a][1];
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d%d", &arr1[i][0], &arr1[i][1]);
  for (int i = 0; i < m; i++) scanf("%d%d", &arr2[i][0], &arr2[i][1]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int k = connect(i, j);
      ans = max(k, ans);
      if (k == 0) continue;
      if (k != num1[i]) {
        cnt1[i]++;
        num1[i] = k;
      }
      if (k != num2[j]) {
        cnt2[j]++;
        num2[j] = k;
      }
    }
  }
  int status = 1;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (cnt1[i] > 1) status++;
    sum += cnt1[i];
  }
  for (int i = 0; i < m; i++) {
    if (cnt2[i] > 1) status++;
    sum += cnt2[i];
  }
  if (status > 1)
    printf("-1\n");
  else {
    if (sum == 2)
      printf("%d\n", ans);
    else {
      int flag = 0;
      int number = 0;
      for (int i = 0; i < n; i++) {
        if (num1[i] == 0) continue;
        if (num1[i] != number) {
          flag++;
          number = num1[i];
        }
      }
      if (flag == 1)
        printf("%d\n", number);
      else
        printf("0\n");
    }
  }
  return 0;
}
