#include <bits/stdc++.h>
using namespace std;
int n, m;
int sh[15][15];
pair<int, int> a[15], b[15];
int main() {
  scanf("%d%d", &n, &m);
  int cnt = 0, p;
  bool ok = 1;
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].first, &b[i].second);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i].first == b[j].first) sh[i][j] = a[i].first;
      if (a[i].second == b[j].second) {
        if (sh[i][j])
          sh[i][j] = -1;
        else
          sh[i][j] = b[j].second;
      }
      if (a[i].first == b[j].second) {
        if (sh[i][j])
          sh[i][j] = -1;
        else
          sh[i][j] = b[j].second;
      }
      if (a[i].second == b[j].first) {
        if (sh[i][j])
          sh[i][j] = -1;
        else
          sh[i][j] = a[i].second;
      }
      if (sh[i][j] > 0) {
        cnt++;
        if (cnt > 1 && p != sh[i][j]) ok = 0;
        p = sh[i][j];
      }
    }
  }
  if (ok) {
    printf("%d\n", p);
    return 0;
  }
  bool f = 1;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    p = 0;
    for (int j = 1; j <= m; j++) {
      if (sh[i][j] > 0) {
        cnt++;
        if (cnt > 1 && p != sh[i][j]) f = 0;
        p = sh[i][j];
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    cnt = 0;
    p = 0;
    for (int j = 1; j <= n; j++) {
      if (sh[j][i] > 0) {
        cnt++;
        if (cnt > 1 && p != sh[j][i]) f = 0;
        p = sh[j][i];
      }
    }
  }
  if (f) {
    printf("0");
    return 0;
  }
  printf("-1");
  return 0;
}
