#include <bits/stdc++.h>
using namespace std;
int read() {
  int k;
  scanf("%d", &k);
  return k;
}
struct x {
  int x, y;
} a[100], b[100];
int flag[100][100];
int A[100], B[100];
int n, m;
int f[100];
int X = -1, num;
int main() {
  int n = read();
  int m = read();
  for (int i = 1; i <= n; i++) {
    a[i].x = read(), a[i].y = read();
    if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
  }
  for (int i = 1; i <= m; i++) {
    b[i].x = read(), b[i].y = read();
    if (b[i].x > b[i].y) swap(b[i].x, b[i].y);
  }
  for (int i = 1; i <= n; i++) {
    memset(f, 0, sizeof(f));
    f[a[i].x] = 1;
    f[a[i].y] = 1;
    int xc = -1;
    for (int j = 1; j <= m; j++) {
      if (a[i].x == b[j].x && a[i].y == b[j].y) continue;
      if (f[b[j].x] == 1) {
        if (xc == -1 || b[j].x != xc) A[i]++, xc = b[j].x;
        if (b[j].x != X) num++, X = b[j].x;
      }
      if (f[b[j].y] == 1) {
        if (xc == -1 || b[j].y != xc) A[i]++, xc = b[j].y;
        if (b[j].y != X) num++, X = b[j].y;
      }
    }
  }
  if (num == 1) {
    cout << X << endl;
    return 0;
  }
  num = 0, X = -1;
  for (int i = 1; i <= m; i++) {
    memset(f, 0, sizeof(f));
    f[b[i].x] = 1;
    f[b[i].y] = 1;
    int xc = -1;
    for (int j = 1; j <= n; j++) {
      if (a[j].x == b[i].x && a[j].y == b[i].y) continue;
      if (f[a[j].x] == 1) {
        if (xc == -1 || a[j].x != xc) B[i]++, xc = a[j].x;
        if (a[j].x != X) num++, X = a[j].x;
      }
      if (f[a[j].y] == 1) {
        if (xc == -1 || a[j].y != xc) B[i]++, xc = a[j].y;
        if (a[j].y != X) num++, X = a[j].y;
      }
    }
  }
  if (num == 1) {
    cout << X << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++)
    if (A[i] >= 2) {
      cout << -1 << endl;
      return 0;
    }
  for (int i = 1; i <= m; i++)
    if (B[i] >= 2) {
      cout << -1 << endl;
      return 0;
    }
  cout << 0 << endl;
  return 0;
}
