#include <bits/stdc++.h>
using namespace std;
int ans1, ans2, n, m;
bool go(int x) {
  if (x % m == 0) {
    return true;
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  ans2 = n / 2;
  ans1 = n - ((n / 2) * 2);
  if (m > n)
    cout << -1;
  else {
    while (!go(ans1 + ans2) && ans2 >= 0 && ans1 >= 0) {
      ans2--;
      ans1 += 2;
    }
    if (ans1 < 0 || ans2 < 0) {
      cout << -1;
      exit(0);
    }
    cout << ans1 + ans2;
  }
  return 0;
}
