#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  long long a, b, c, d;
  vector<long long> x(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%lld.%lld.%lld.%lld", &a, &b, &c, &d);
    x[i] = (a << 24) + (b << 16) + (c << 8) + d;
  }
  long long t = 0;
  for (int i = 31; i >= 1; i--) {
    t += 1ll << i;
    set<long long> s;
    for (int j = 1; j <= n; j++) {
      s.insert(t & x[j]);
    }
    if ((int)(s).size() == k) {
      d = t - ((t >> 8) << 8);
      t >>= 8;
      c = t - ((t >> 8) << 8);
      t >>= 8;
      b = t - ((t >> 8) << 8);
      t >>= 8;
      a = t;
      printf("%lld.%lld.%lld.%lld", a, b, c, d);
      return 0;
    }
  }
  cout << -1 << '\n';
  return 0;
}
