#include <bits/stdc++.h>
using namespace std;
int common(pair<int, int> p, pair<int, int> q) {
  if ((p.first == q.first and p.second == q.second) or
      (p.first == q.second and p.second == q.first)) {
    return -1;
  }
  if (p.first == q.first or p.first == q.second) {
    return p.first;
  }
  if (p.second == q.first or p.second == q.second) {
    return p.second;
  }
  return -1;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  pair<int, int> np[20];
  pair<int, int> mp[20];
  for (int i = 0; i < n; i++) {
    cin >> np[i].first >> np[i].second;
  }
  for (int i = 0; i < m; i++) {
    cin >> mp[i].first >> mp[i].second;
  }
  int ans = 1;
  int number = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (number == -1) {
        number = common(np[i], mp[j]);
      }
      if (number != common(np[i], mp[j]) and common(np[i], mp[j]) != -1) {
        number = common(np[i], mp[j]);
        ans = 0;
      }
    }
  }
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      if (number == -1) {
        number = common(np[i], mp[j]);
      }
      if (number != common(np[i], mp[j]) and common(np[i], mp[j]) != -1) {
        number = common(np[i], mp[j]);
        ans = 0;
      }
    }
  }
  if (ans == 0) {
    for (int i = 0; i < n; i++) {
      int curr = -1;
      int first = 0;
      for (int j = 0; j < m; j++) {
        if (common(np[i], mp[j]) != -1) {
          first++;
          if (first == 1) {
            curr = common(np[i], mp[j]);
          } else {
            if (curr != common(np[i], mp[j])) {
              ans = -1;
            }
          }
        }
      }
    }
    for (int j = 0; j < m; j++) {
      int curr = -1;
      int first = 0;
      for (int i = 0; i < n; i++) {
        if (common(np[i], mp[j]) != -1) {
          first++;
          if (first == 1) {
            curr = common(np[i], mp[j]);
          } else {
            if (curr != common(np[i], mp[j])) {
              ans = -1;
            }
          }
        }
      }
    }
    cout << ans;
  } else {
    cout << number;
  }
  return 0;
}
