#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = INT_MAX;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n / 2 + 1; i++) {
    if ((n - i) % m == 0) {
      int x = n - 2 * i;
      if (x >= 0) {
        ans = min(ans, x + i);
      }
    }
  }
  if (ans == INT_MAX)
    cout << -1;
  else
    cout << ans;
}
