#include <bits/stdc++.h>
int n, m, a[20], b[20];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[i] = 1 << x | 1 << y;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    b[i] = 1 << x | 1 << y;
  }
  int ans1 = 0, ans0 = 1;
  for (int i = 0; i < n; i++) {
    int cur = 0;
    for (int j = 0; j < m; j++)
      if (a[i] != b[j] && (a[i] & b[j])) {
        int c = a[i] & b[j];
        if (!cur)
          cur = c;
        else if (c != cur)
          cur = -1;
      }
    if (!cur) continue;
    if (!ans1)
      ans1 = cur;
    else if (cur != ans1)
      ans1 = -1;
    if (cur < 0) ans0 = 0;
  }
  for (int j = 0; j < m; j++) {
    int cur = 0;
    for (int i = 0; i < n; i++)
      if (a[i] != b[j] && (a[i] & b[j])) {
        int c = a[i] & b[j];
        if (!cur)
          cur = c;
        else if (c != cur)
          cur = -1;
      }
    if (!cur) continue;
    if (!ans1)
      ans1 = cur;
    else if (cur != ans1)
      ans1 = -1;
    if (cur < 0) ans0 = 0;
  }
  if (ans1 > 0)
    printf("%d\n", __builtin_ctz(ans1));
  else if (ans0)
    puts("0");
  else
    puts("-1");
}
