#include <bits/stdc++.h>
using namespace std;
unsigned int a[112345];
int main() {
  int k, n;
  cin >> n >> k;
  for (int z = 1; z <= n; z++) {
    string s;
    cin >> s;
    int sl = s.length();
    unsigned int all = 0;
    unsigned int now = 0;
    for (int i = 0; i <= sl; i++) {
      if (i == sl || s[i] == '.') {
        all <<= 8;
        all += now;
        now = 0;
      } else {
        now = now * 10 + (s[i] - '0');
      }
    }
    a[z] = all;
  }
  unsigned int t = 0;
  for (int i = 31; i >= 1; i--) {
    t |= (1LL << i);
    set<unsigned int> s;
    for (int j = 1; j <= n; j++) {
      s.insert(t & a[j]);
    }
    if (s.size() == k) {
      unsigned int z4 = t & 255;
      t >>= 8;
      unsigned int z3 = t & 255;
      t >>= 8;
      unsigned int z2 = t & 255;
      t >>= 8;
      unsigned int z1 = t & 255;
      t >>= 8;
      cout << z1 << "." << z2 << "." << z3 << "." << z4 << "\n";
      return 0;
    }
  }
  cout << -1 << "\n";
  return 0;
}
