#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 * 1000 + 100;
set<long long> st;
string s[maxn], t;
long long a[maxn][5], b[maxn];
long long n, k;
void output() {
  long long p = 0;
  for (long long i = 0; i < 8; i++) p = p * 2LL + t[i] - '0';
  cout << p << '.';
  p = 0;
  for (long long i = 8; i < 16; i++) p = p * 2LL + t[i] - '0';
  cout << p << '.';
  p = 0;
  for (long long i = 16; i < 24; i++) p = p * 2LL + t[i] - '0';
  cout << p << '.';
  p = 0;
  for (long long i = 24; i < 32; i++) p = p * 2LL + t[i] - '0';
  cout << p << endl;
}
int main() {
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
    long long r = 0;
    for (long long j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '.')
        r++;
      else
        a[i][r] = a[i][r] * 10 + s[i][j] - '0';
    }
    b[i] = a[i][0] * (1LL << 24) + a[i][1] * (1LL << 16) +
           a[i][2] * (1LL << 8) + a[i][3];
  }
  for (long long i = 31; i >= 1; i--) {
    for (long long j = 0; j < n; j++) st.insert(b[j] / (1LL << i));
    if (st.size() == k) {
      for (long long g = 0; g < 32 - i; g++) t.push_back('1');
      for (long long g = 32 - i; g < 32; g++) t.push_back('0');
      output();
      return 0;
    }
    if (st.size() > k) break;
    st.clear();
  }
  cout << -1 << endl;
  return 0;
}
