#include <bits/stdc++.h>
using namespace std;
int const N = 200002;
int const MD = 1000000007;
int a1[20], a2[20], b1[20], b2[20];
int m1[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int(i) = (1); (i) <= (n); ++(i)) cin >> a1[i] >> a2[i];
  for (int(i) = (1); (i) <= (m); ++(i)) cin >> b1[i] >> b2[i];
  int d1[10] = {}, d2[10] = {};
  for (int(i) = (1); (i) <= (n); ++(i)) {
    int a = 0, b = 0;
    for (int(j) = (1); (j) <= (m); ++(j)) {
      if (a1[i] == b1[j] && a2[i] == b2[j]) {
        continue;
      }
      if (a1[i] == b2[j] && a2[i] == b1[j]) {
        continue;
      }
      if (a1[i] == b1[j] || a1[i] == b2[j]) {
        d1[a1[i]] = 1;
        a = 1;
      }
      if (a2[i] == b2[j] || a2[i] == b1[j]) {
        d1[a2[i]] = 1;
        b = 1;
      }
    }
    if (a == 1 && b == 1) {
      cout << "-1\n";
      return 0;
    }
  }
  for (int(j) = (1); (j) <= (m); ++(j)) {
    int a = 0, b = 0;
    for (int(i) = (1); (i) <= (n); ++(i)) {
      if (a1[i] == b1[j] && a2[i] == b2[j]) {
        continue;
      }
      if (a1[i] == b2[j] && a2[i] == b1[j]) {
        continue;
      }
      if (a1[i] == b1[j] || b1[j] == a2[i]) {
        d2[b1[j]] = 1;
        a = 1;
      }
      if (a2[i] == b2[j] || a1[i] == b2[j]) {
        d2[b2[j]] = 1;
        b = 1;
      }
    }
    if (a == 1 && b == 1) {
      cout << "-1\n";
      return 0;
    }
  }
  {
    int sum = 0, who = 0;
    for (int i = 1; i < 10; ++i) {
      sum += d1[i];
      if (d1[i]) who = i;
    }
    if (sum == 1) {
      cout << who << "\n";
      return 0;
    }
  }
  {
    int sum = 0, who = 0;
    for (int i = 1; i < 10; ++i) {
      sum += d2[i];
      if (d2[i]) who = i;
    }
    if (sum == 1) {
      cout << who << "\n";
      return 0;
    }
  }
  cout << "0\n";
  return 0;
}
