#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const double PI = 3.14159265359;
bool check(pair<int, int> p1, pair<int, int> p2) {
  return p1.first == p2.first || p1.second == p2.second ||
         p1.first == p2.second || p1.second == p2.first;
}
int getComm(pair<int, int> p1, pair<int, int> p2) {
  return p1.first == p2.first || p1.first == p2.second ? p1.first : p1.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> vp1(n), vp2(m);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    vp1[i] = {min(x, y), max(x, y)};
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    vp2[i] = {min(x, y), max(x, y)};
  }
  vector<vector<int>> gr(n + m);
  set<int> cands;
  int ect = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vp1[i] == vp2[j]) continue;
      if (check(vp1[i], vp2[j])) {
        gr[i].push_back(n + j);
        gr[n + j].push_back(i);
        cands.insert(getComm(vp1[i], vp2[j]));
      }
    }
  }
  if (cands.size() == 1) {
    cout << *cands.begin();
    return 0;
  }
  for (int i = 0; i < n + m; i++) {
    bool p1 = false, p2 = false;
    for (int j : gr[i]) {
      if (i < n) {
        int val = getComm(vp1[i], vp2[j - n]);
        if (val == vp1[i].first)
          p1 = true;
        else
          p2 = true;
      } else {
        int val = getComm(vp2[i - n], vp1[j]);
        if (val == vp2[i - n].first)
          p1 = true;
        else
          p2 = true;
      }
    }
    if (p1 && p2) {
      cout << -1;
      return 0;
    }
  }
  cout << 0;
  return 0;
}
