#include <bits/stdc++.h>
using namespace std;
pair<int, int> v[100005], w[100005];
int main() {
  int n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
  for (i = 0; i < m; i++) cin >> w[i].first >> w[i].second;
  bool multiple = false, determine = true;
  int exists = 0, ans;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (v[i] == w[j]) continue;
      if (v[i].second == w[j].first && v[i].first == w[j].second) continue;
      if (v[i].first == v[i].second || w[j].first == w[j].second) continue;
      int same = v[i].first;
      if (w[j].first != same && w[j].second != same) {
        same = v[i].second;
        if (w[j].first != same && w[j].second != same) continue;
      }
      exists++;
      if (exists == 1)
        ans = same;
      else if (ans != same)
        determine = false;
      for (k = 0; k < m; k++) {
        if (k == j) continue;
        if (w[k].first == v[i].first + v[i].second - same) {
          if (w[k].second != same) multiple = true;
        }
        if (w[k].second == v[i].first + v[i].second - same) {
          if (w[k].first != same) multiple = true;
        }
      }
      for (k = 0; k < n; k++) {
        if (k == i) continue;
        if (v[k].first == w[j].first + w[j].second - same) {
          if (v[k].second != same) multiple = true;
        }
        if (v[k].second == w[j].first + w[j].second - same) {
          if (v[k].first != same) multiple = true;
        }
      }
    }
  }
  if (exists == 0)
    cout << "-1\n";
  else if (determine)
    cout << ans << '\n';
  else if (!multiple)
    cout << "0\n";
  else
    cout << "-1\n";
  return 0;
}
