#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[12 + 1];
pair<int, int> b[12 + 1];
int common[12 + 1];
int N, M;
int areCommon(int i, int j) {
  if (a[i].first == b[j].first && a[i].second != b[j].second) return a[i].first;
  if (a[i].first != b[j].first && a[i].second == b[j].second)
    return a[i].second;
  if (a[i].first == b[j].second && a[i].second != b[j].first) return a[i].first;
  if (a[i].second == b[j].first && a[i].first != b[j].second)
    return a[i].second;
  return 0;
}
int main() {
  int i, j, k, posib = 0, res;
  cin >> N >> M;
  for (i = 1; i <= N; i++) cin >> a[i].first >> a[i].second;
  for (i = 1; i <= M; i++) cin >> b[i].first >> b[i].second;
  for (i = 1; i <= N; i++)
    for (j = 1; j <= M; j++)
      if (areCommon(i, j)) {
        common[areCommon(i, j)] = 1;
      } else if (areCommon(i, j)) {
        common[areCommon(i, j)] = 1;
      }
  for (i = 1; i <= 9; i++)
    if (common[i]) ++posib, res = i;
  if (posib == 1) {
    cout << res;
    return 0;
  }
  int good = 1;
  for (i = 1; i <= N; i++)
    for (j = 1; j <= M; j++)
      if (areCommon(i, j)) {
        memset(common, 0, sizeof(common));
        for (k = 1; k <= M; k++)
          if (areCommon(i, k)) common[areCommon(i, k)] = 1;
        posib = 0;
        for (k = 1; k <= 9; k++)
          if (common[k]) ++posib;
        if (posib > 1) good = 0;
        memset(common, 0, sizeof(common));
        for (k = 1; k <= N; k++)
          if (areCommon(k, j)) common[areCommon(k, j)] = 1;
        posib = 0;
        for (k = 1; k <= 9; k++)
          if (common[k]) ++posib;
        if (posib > 1) good = 0;
      }
  if (good == 1)
    cout << "0\n";
  else
    cout << "-1\n";
  return 0;
}
