#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> a[15], b[15];
int statA[15], statB[15];
vector<int> AtoB[15];
bool ans[10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i <= n - 1; i++) cin >> a[i].first >> a[i].second;
  for (int i = 0; i <= m - 1; i++) cin >> b[i].first >> b[i].second;
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j <= m - 1; j++) {
      if ((a[i].first == b[j].first && a[i].second != b[j].second) ||
          (a[i].first == b[j].second && a[i].second != b[j].first)) {
        if (statA[i] != 0) {
          if (statA[i] != a[i].first)
            statA[i] = -1;
          else
            AtoB[i].push_back(j);
        } else {
          statA[i] = a[i].first;
          AtoB[i].push_back(j);
        }
        if (statB[j] != 0) {
          if (statB[j] != a[i].first) statB[j] = -1;
        } else {
          statB[j] = a[i].first;
        }
      }
      if ((a[i].second == b[j].first && a[i].first != b[j].second) ||
          (a[i].second == b[j].second && a[i].first != b[j].first)) {
        if (statA[i] != 0) {
          if (statA[i] != a[i].second)
            statA[i] = -1;
          else
            AtoB[i].push_back(j);
        } else {
          statA[i] = a[i].second;
          AtoB[i].push_back(j);
        }
        if (statB[j] != 0) {
          if (statB[j] != a[i].second) statB[j] = -1;
        } else {
          statB[j] = a[i].second;
        }
      }
    }
  }
  int anscnt = 0;
  bool chaos = false;
  for (int i = 0; i <= n - 1; i++) {
    if (statA[i] > 0) {
      for (auto j : AtoB[i]) {
        if (statB[j] > 0) {
          ans[statA[i]] = true;
          break;
        } else if (statB[j] < 0)
          chaos = true;
      }
    } else if (statA[i] < 0)
      chaos = true;
  }
  int finalans = -1;
  for (int i = 1; i <= 9; i++) {
    if (ans[i]) {
      anscnt++;
      finalans = i;
    }
  }
  if (chaos)
    cout << -1 << '\n';
  else if (anscnt > 1)
    cout << 0 << '\n';
  else if (anscnt == 1)
    cout << finalans << '\n';
  else
    cout << -1 << '\n';
  return 0;
}
