#include <bits/stdc++.h>
using namespace std;
int static const N = 200010;
int n, m;
vector<int> brojke[40], bb2[40];
pair<int, int> a[30], b[30];
int razl[100];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    a[i] = pair<int, int>(x, y);
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    b[i] = pair<int, int>(x, y);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i].first == b[j].first && a[i].second == b[j].second) continue;
      if (a[i].first == b[j].first || a[i].first == b[j].second) {
        bb2[j].push_back(a[i].first);
        brojke[i].push_back(a[i].first);
      }
      if (a[i].second == b[j].second || a[i].second == b[j].first) {
        brojke[i].push_back(a[i].second);
        bb2[j].push_back(a[i].second);
      }
    }
  }
  int brRazl = 0, t;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < brojke[i].size(); j++) {
      if (razl[brojke[i][j]] == 0) brRazl++;
      razl[brojke[i][j]] = 1;
      t = brojke[i][j];
    }
  }
  if (brRazl == 1)
    printf("%i\n", t);
  else {
    bool rr = true;
    for (int i = 0; i < n; i++) {
      sort(brojke[i].begin(), brojke[i].end());
      int sn = brojke[i].size();
      if (sn && brojke[i][0] != brojke[i][sn - 1]) rr = false;
    }
    for (int i = 0; i < m; i++) {
      sort(bb2[i].begin(), bb2[i].end());
      int sn = bb2[i].size();
      if (sn && bb2[i][0] != bb2[i][sn - 1]) rr = false;
    }
    if (rr)
      printf("0\n");
    else
      printf("-1\n");
  }
  return 0;
}
