#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
  for (int(i) = 0; (i) < (((int)((t).size()))); ++(i)) s << t[i] << " ";
  return s;
}
int main() {
  vector<unsigned> v;
  int(n), (k);
  scanf("%d %d", &(n), &(k));
  for (int(i) = 0; (i) < (n); ++(i)) {
    int x1, x2, x3, x4;
    scanf("%d.%d.%d.%d", &x1, &x2, &x3, &x4);
    unsigned x = x1;
    x = 256 * x + x2;
    x = 256 * x + x3;
    x = 256 * x + x4;
    v.push_back(x);
  }
  sort(v.begin(), v.end());
  for (int(ones) = (1); (ones) <= (31); ++ones) {
    unsigned mask = (1LL << 32) - (1LL << (32 - ones));
    int cnt = 1;
    for (int(i) = (1); (i) <= (((int)((v).size())) - 1); ++i)
      if ((v[i] & mask) != (v[i - 1] & mask)) ++cnt;
    if (cnt == k) {
      int x1 = mask >> 24, x2 = (mask >> 16) & 255, x3 = (mask >> 8) & 255,
          x4 = mask & 255;
      printf("%d.%d.%d.%d", x1, x2, x3, x4);
      return 0;
    }
  }
  pisz(-1);
}
