#include <bits/stdc++.h>
using namespace std;
const int MaxN = 20;
int n, m;
int A[MaxN][2], B[MaxN][2];
set<int> aa[MaxN], bb[MaxN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < 2; ++j) scanf("%d", &A[i][j]);
  for (int i = 1; i <= m; ++i)
    for (int j = 0; j < 2; ++j) scanf("%d", &B[i][j]);
  set<int> S;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int ii = 0; ii < 2; ++ii)
        for (int jj = 0; jj < 2; ++jj)
          if (A[i][ii] == B[j][jj] && A[i][1 - ii] != B[j][1 - jj]) {
            S.insert(A[i][ii]);
            aa[i].insert(A[i][ii]);
            bb[j].insert(A[i][ii]);
          }
  if (S.size() == 1) {
    printf("%d\n", *S.begin());
  } else {
    bool flag = true;
    for (int i = 0; i <= n; ++i)
      if (aa[i].size() >= 2) flag = false;
    for (int i = 0; i <= m; ++i)
      if (bb[i].size() >= 2) flag = false;
    if (flag)
      puts("0");
    else
      puts("-1");
  }
}
