#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr[2][15];
int cnt[2][15];
vector<int> vec;
bool can(pair<int, int> a, pair<int, int> b) {
  int ans = 0;
  if (a.first == b.first || a.first == b.second) ans++;
  if (a.second == b.first || a.second == b.second) ans++;
  return (ans == 1);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d %d", &arr[0][i].first, &arr[0][i].second);
  for (int i = 0; i < m; i++)
    scanf("%d %d", &arr[1][i].first, &arr[1][i].second);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ((arr[0][i].first == arr[1][j].first ||
           arr[0][i].first == arr[1][j].second) &&
          can(arr[0][i], arr[1][j])) {
        cnt[0][i]++;
        break;
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ((arr[0][i].second == arr[1][j].first ||
           arr[0][i].second == arr[1][j].second) &&
          can(arr[0][i], arr[1][j])) {
        cnt[0][i]++;
        break;
      }
  for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++)
      if ((arr[1][j].first == arr[0][i].first ||
           arr[1][j].first == arr[0][i].second) &&
          can(arr[0][i], arr[1][j])) {
        cnt[1][j]++;
        break;
      }
  for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++)
      if ((arr[1][j].second == arr[0][i].first ||
           arr[1][j].second == arr[0][i].second) &&
          can(arr[0][i], arr[1][j])) {
        cnt[1][j]++;
        break;
      }
  int me = 1, a = 1, b = 1, las;
  for (int i = 0; i < n; i++)
    if (cnt[0][i] == 2) a = 0;
  for (int j = 0; j < m; j++)
    if (cnt[1][j] == 2) b = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (can(arr[0][i], arr[1][j])) {
        if (arr[0][i].first == arr[1][j].first ||
            arr[0][i].first == arr[1][j].second)
          vec.push_back(arr[0][i].first);
        if (arr[0][i].second == arr[1][j].first ||
            arr[0][i].second == arr[1][j].second)
          vec.push_back(arr[0][i].second);
      }
  if (vec.size() == 0) me = 0;
  for (int i = 0; i < (int)vec.size() - 1; i++)
    if (vec[i] != vec[i + 1]) me = 0;
  if (me)
    printf("%d\n", vec[0]);
  else if (a && b)
    printf("0\n");
  else
    printf("-1\n");
  return 0;
}
