#include <bits/stdc++.h>
using namespace std;
int pa[15][2], pb[15][2];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &pa[i][0], &pa[i][1]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &pb[i][0], &pb[i][1]);
  }
  int ans = 10;
  for (int i = 0; i < n; i++) {
    int nowans = 10;
    for (int k = 0; k < 2; k++) {
      int f = 0;
      for (int j = 0; j < m; j++) {
        for (int l = 0; l < 2; l++) {
          if (pa[i][k] == pb[j][l]) {
            if (pa[i][1 - k] == pb[j][1 - l]) break;
            if (nowans == 10)
              nowans = pa[i][k];
            else
              nowans = -1;
            f = 1;
            break;
          }
        }
        if (f) break;
      }
    }
    if (nowans == -1)
      ans = -1;
    else if (ans == 10)
      ans = nowans;
    else if (nowans != 10 && nowans != ans && ans != -1)
      ans = 0;
  }
  int ans2 = 10;
  for (int i = 0; i < m; i++) {
    int nowans = 10;
    for (int k = 0; k < 2; k++) {
      int f = 0;
      for (int j = 0; j < n; j++) {
        for (int l = 0; l < 2; l++) {
          if (pb[i][k] == pa[j][l]) {
            if (pb[i][1 - k] == pa[j][1 - l]) break;
            if (nowans == 10)
              nowans = pb[i][k];
            else
              nowans = -1;
            f = 1;
            break;
          }
        }
        if (f) break;
      }
    }
    if (nowans == -1)
      ans2 = -1;
    else if (ans2 == 10)
      ans2 = nowans;
    else if (nowans != 10 && nowans != ans2 && ans2 != -1)
      ans2 = 0;
  }
  if (ans == -1 || ans2 == -1)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
