#include <bits/stdc++.h>
using namespace std;
bool obvious(vector<int>& possible) {
  int nz = 0;
  for (auto n : possible) {
    if (n > 0) {
      ++nz;
    }
  }
  return nz == 1;
}
bool all_zero(vector<int>& possible) {
  for (auto n : possible) {
    if (n > 0) {
      return false;
    }
  }
  return true;
}
bool different(pair<int, int>& a, pair<int, int>& b) {
  return a != b && !(a.first == b.second && a.second == b.first);
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> p1;
  vector<pair<int, int>> p2;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a;
    cin >> b;
    p1.push_back(make_pair(a, b));
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a;
    cin >> b;
    p2.push_back(make_pair(a, b));
  }
  vector<int> possible(10, 0);
  for (auto a : p1) {
    for (auto b : p2) {
      if (different(a, b)) {
        if (a.first == b.first || a.first == b.second) {
          ++possible[a.first];
        } else if (a.second == b.first || a.second == b.second) {
          ++possible[a.second];
        }
      }
    }
  }
  if (obvious(possible)) {
    for (int i = 0; i < possible.size(); ++i) {
      if (possible[i]) {
        cout << i << endl;
      }
    }
    return 0;
  }
  bool always_obvious = true;
  for (auto a : p1) {
    vector<int> possible(10, 0);
    for (auto b : p2) {
      if (different(a, b)) {
        if (a.first == b.first || a.first == b.second) {
          ++possible[a.first];
        } else if (a.second == b.first || a.second == b.second) {
          ++possible[a.second];
        }
      }
    }
    if (!(obvious(possible) || all_zero(possible))) {
      always_obvious = false;
    }
  }
  for (auto a : p2) {
    vector<int> possible(10, 0);
    for (auto b : p1) {
      if (different(a, b)) {
        if (a.first == b.first || a.first == b.second) {
          ++possible[a.first];
        } else if (a.second == b.first || a.second == b.second) {
          ++possible[a.second];
        }
      }
    }
    if (!(obvious(possible) || all_zero(possible))) {
      always_obvious = false;
    }
  }
  if (always_obvious) {
    cout << "0" << endl;
    return 0;
  }
  cout << "-1" << endl;
  return 0;
}
