#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, a[15][2], b[15][2], pre, tot = 0, x, mark[10] = {0};
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; ++i) scanf("%d %d", &a[i][0], &a[i][1]);
  for (i = 0; i < m; ++i) scanf("%d %d", &b[i][0], &b[i][1]);
  for (i = 0; i < n; ++i) {
    int cnt = 0, mark2[10] = {0};
    vector<int> vec;
    for (j = 0; j < m; ++j) {
      if (a[i][0] == b[j][0] && a[i][1] != b[j][1]) {
        if (!mark2[b[j][0]]) cnt++;
        mark2[b[j][0]]++;
        vec.push_back(j);
      }
      if (a[i][0] == b[j][1] && a[i][1] != b[j][0]) {
        if (!mark2[b[j][1]]) cnt++;
        mark2[b[j][1]]++;
        vec.push_back(j);
      }
      if (a[i][1] == b[j][0] && a[i][0] != b[j][1]) {
        if (!mark2[b[j][0]]) cnt++;
        mark2[b[j][0]]++;
        vec.push_back(j);
      }
      if (a[i][1] == b[j][1] && a[i][0] != b[j][0]) {
        if (!mark2[b[j][1]]) cnt++;
        mark2[b[j][1]]++;
        vec.push_back(j);
      }
    }
    if (cnt > 1) break;
    memset(mark2, 0, sizeof(mark2));
    if (cnt == 1) {
      cnt = 0;
      for (pre = 0; pre < vec.size(); ++pre) {
        for (j = 0; j < n; ++j) {
          if (a[j][0] == b[vec[pre]][0] && a[j][1] != b[vec[pre]][1]) {
            if (!mark2[a[j][0]]) cnt++;
            mark2[a[j][0]]++;
          }
          if (a[j][0] == b[vec[pre]][1] && a[j][1] != b[vec[pre]][0]) {
            if (!mark2[a[j][0]]) cnt++;
            mark2[a[j][0]]++;
          }
          if (a[j][1] == b[vec[pre]][0] && a[j][0] != b[vec[pre]][1]) {
            if (!mark2[a[j][1]]) cnt++;
            mark2[a[j][1]]++;
          }
          if (a[j][1] == b[vec[pre]][1] && a[j][0] != b[vec[pre]][0]) {
            if (!mark2[a[j][1]]) cnt++;
            mark2[a[j][1]]++;
          }
        }
        if (cnt > 1)
          break;
        else {
          for (j = 1; j < 10; ++j) {
            if (mark2[j]) x = j;
          }
        }
      }
      if (pre < vec.size())
        break;
      else {
        if (!mark[x]) tot++;
        mark[x]++;
      }
    }
  }
  if (i < n)
    printf("-1\n");
  else if (tot > 1)
    printf("0\n");
  else {
    for (i = 1; i < 10; ++i) {
      if (mark[i]) {
        printf("%d\n", i);
        break;
      }
    }
  }
  return 0;
}
