#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000 * 1000 * 1000 + 1;
const long long inf = 1ll * 1000 * 1000 * 1000 * 10 + 7;
const long long MOD2 = 998244353;
const long long N = 1000 * 1000 + 10;
const long long N2 = 70;
const long double PI = 3.14159265358979323846;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void solve() {}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  long long ip[n];
  for (long long i = 0; i < n; i++) {
    long long A, B, C, D;
    char a;
    cin >> A >> a >> B >> a >> C >> a >> D;
    ip[i] = ((A << 24) + (B << 16) + (C << 8) + D);
  }
  for (long long i = 1; i < 32; i++) {
    long long mask = (1 << i) - 1;
    mask <<= (32 - i);
    long long netmsk[n];
    for (long long j = 0; j < n; j++) {
      netmsk[j] = (ip[j] & mask);
    }
    sort(netmsk, netmsk + n);
    long long u = unique(netmsk, netmsk + n) - netmsk;
    if (u == k) {
      long long a, b, c, d;
      d = mask & 255;
      mask >>= 8;
      c = mask & 255;
      mask >>= 8;
      b = mask & 255;
      mask >>= 8;
      a = mask;
      cout << a << "." << b << "." << c << "." << d;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
