#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int N = 100000;
const int S = 32;
long long a[N], p[S];
int main() {
  p[0] = 1;
  for (int i = 1; i < S; i++) p[i] = p[i - 1] * 2;
  long long ans = -1, ansL = INF;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    long long q, w, e, r;
    scanf("%I64d.%I64d.%I64d.%I64d", &q, &w, &e, &r);
    a[i] = q * p[24] + w * p[16] + e * p[8] + r;
  }
  long long now = 0;
  set<long long> masks;
  for (int i = S - 1; i > 0; i--) {
    now += p[i];
    for (int j = 0; j < n; j++) masks.insert(a[j] & now);
    if (masks.size() == k && (S - 1) - i + 1 < ansL) {
      ansL = (S - 1) - i + 1;
      ans = now;
    }
    masks.clear();
  }
  if (ans == -1) {
    cout << -1;
    return 0;
  }
  for (int i = 3; i >= 0; i--) {
    cout << ans / p[i * 8];
    ans %= p[i * 8];
    if (i) cout << '.';
  }
  return 0;
}
