#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 100000000;
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
int ip[((int)1e5)];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < (n); i++) {
    int a, b, c, d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    ip[i] = (a << 24) + (b << 16) + (c << 8) + d;
  }
  unsigned int ans = 1 << 31;
  while (ans != -1) {
    set<int> st;
    for (int i = 0; i < (n); i++) st.insert(ip[i] & ans);
    if (((int)(st).size()) == k) break;
    ans = (ans >> 1) | (1 << 31);
  }
  if (ans != -1)
    printf("%d.%d.%d.%d\n", (ans >> 24), (ans >> 16) & 0xFF, (ans >> 8) & 0xFF,
           ans & 0xFF);
  else
    puts("-1");
  return 0;
}
