#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  assert(n >= 1 && n <= 10000 && m >= 1 && m <= 10);
  if (n == m && (n == 1 || n == 2))
    cout << n << endl;
  else {
    if (n == m)
      cout << n << endl;
    else {
      int t2 = 0, t1 = 0;
      if (n & 1) {
        n--;
        t1++;
      }
      t2 = n / 2;
      while ((t2 + t1) % m != 0 && n > 0) {
        n = n - 2;
        t1 = t1 + 2;
        t2 = n / 2;
      }
      if (n == 0)
        cout << -1 << endl;
      else
        cout << t1 + t2 << endl;
    }
  }
  return 0;
}
