#include <bits/stdc++.h>
using namespace std;
string tostr(unsigned int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
unsigned int toint(string &s) {
  stringstream ss;
  ss << s;
  long long x;
  ss >> x;
  return x;
}
int countset(unsigned int mask);
string bin(unsigned int n);
string toip(unsigned int mask);
int countdis(unsigned int mask);
int n, k;
unsigned int ip[100001] = {0};
int main() {
  int pok;
  cin >> n >> pok;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    s += '.';
    unsigned int no = 0;
    unsigned int as[4] = {0};
    int kk = 0;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '.') {
        as[kk++] |= no;
        no = 0;
      } else {
        no = no * 10 + s[j] - '0';
      }
    }
    ip[i] = 0;
    for (int ii = 0, shift = 24; ii < 4; ii++) {
      ip[i] |= (as[ii]) << shift;
      shift -= 8;
    }
  }
  sort(ip, ip + n);
  unsigned int mask = 0;
  unsigned int best = 5;
  unsigned int bestones = 33;
  bool found = false;
  mask = 0;
  int co1 = countdis(~mask);
  int setbits1 = countset(~mask);
  if (co1 == pok) {
    best = ~mask;
    bestones = 0;
  }
  for (unsigned int i = 0; i < 32; i++) {
    mask |= (1 << i);
    if (~mask == 0) continue;
    int co = countdis(~mask);
    int setbits = countset(~mask);
    if (co == pok && found == false) {
      best = ~mask;
      found = true;
      bestones = setbits;
    } else if (co == pok && found == true) {
      if (bestones > setbits) {
        best = ~mask;
        bestones = setbits;
      }
    }
  }
  if (!found)
    cout << -1;
  else {
    cout << toip(best) << endl;
  }
  return 0;
}
string bin(unsigned int n) {
  string s(32, '0');
  int i = 31;
  while (n) {
    s[i--] = (n % 2) + '0';
    n = n / 2;
  }
  return s;
}
string toip(unsigned int mask) {
  string res = "";
  unsigned int no;
  no = 0;
  string bb = bin(mask);
  int b[32] = {0};
  for (int i = 0; i < 32; i++) b[i] = bb[31 - i] - '0';
  int shift = 0;
  for (unsigned int i = 24; i < 32; i++) {
    no |= ((1 << (i - 24)) * b[i]);
  }
  res += tostr(no) + '.';
  no = 0;
  for (unsigned int i = 16; i < 24; i++) {
    no |= ((1 << (i - 16)) * b[i]);
  }
  res += tostr(no) + '.';
  no = 0;
  for (unsigned int i = 8; i < 16; i++) {
    no |= ((1 << (i - 8)) * b[i]);
  }
  res += tostr(no) + '.';
  no = 0;
  for (unsigned int i = 0; i < 8; i++) {
    no |= ((1 << (i)) * b[i]);
  }
  res += tostr(no);
  no = 0;
  return res;
}
int countdis(unsigned int mask) {
  map<unsigned int, int> h;
  int c = 0;
  for (int i = 0; i < n; i++) {
    unsigned int id = mask & ip[i];
    if (h.find(id) == h.end()) {
      h[id] = 1;
      c++;
    }
  }
  return c;
}
int countset(unsigned int mask) {
  int c = 0;
  for (unsigned int i = 0; i < 32; i++) {
    if (mask & (1 << i)) c++;
  }
  return c;
}
