#include <bits/stdc++.h>
using namespace std;
int n = 0, k = 0;
bool ip[100010][32];
int main() {
  n = 0, k = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a = 0, b = 0, c = 0, d = 0;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    int l = 31;
    while (d) {
      ip[i][l--] = d % 2;
      d /= 2;
    }
    l = 23;
    while (c) {
      ip[i][l--] = c % 2;
      c /= 2;
    }
    l = 15;
    while (b) {
      ip[i][l--] = b % 2;
      b /= 2;
    }
    l = 7;
    while (a) {
      ip[i][l--] = a % 2;
      a /= 2;
    }
  }
  set<string> net;
  int masklen = 1;
  bool mask[32];
  for (int i = 0; i < 32; i++) mask[i] = 0;
  while (net.size() != k && masklen <= 32) {
    net.clear();
    mask[masklen - 1] = 1;
    string netip = "00000000000000000000000000000000";
    for (int j = 0; j < n; j++) {
      for (int i = 0; i < 32; i++) {
        netip[i] = mask[i] && ip[j][i];
      }
      net.insert(netip);
    }
    masklen++;
  }
  if (net.size() == k) {
    if (masklen != 33) {
      int i;
      for (i = 0; i < (masklen - 1) / 8; i++) cout << "255.";
      int p = 0;
      for (int e = 0; e < (masklen - 1) % 8; e++) {
        p += (1 << (7 - e));
      }
      cout << p;
      if (i == 3) return 0;
      for (; i < 3; i++) cout << ".0";
    } else
      cout << "255.255.255.255";
  } else
    cout << "-1";
  return 0;
}
