#include <bits/stdc++.h>
using namespace std;
int a[100004];
int t[4];
int main() {
  int n, k;
  cin >> n >> k;
  for (long long int i = 0; i < int(n); ++i) {
    scanf("%d.%d.%d.%d", &t[0], &t[1], &t[2], &t[3]);
    for (int j = 0; j < 4; ++j) a[i] |= t[j] << (8 * (3 - j));
  }
  int mask = 0;
  for (int i = 31; i >= 0; --i) {
    mask |= (1 << i);
    set<int> s;
    for (long long int j = 0; j < int(n); ++j) {
      s.insert(mask & a[j]);
      if (s.size() > k) {
        cout << -1;
        return 0;
      }
    }
    if (s.size() == k) {
      int i = 3;
      for (long long int tt = 0; tt < int(4); ++tt) t[tt] = 0;
      while (mask && i > -1) {
        t[i] = mask & 0xFF;
        mask >>= 8;
        --i;
      }
      printf("%d.%d.%d.%d", t[0], t[1], t[2], t[3]);
      return 0;
    }
  }
  cout << -1;
  return 0;
}
