#include <bits/stdc++.h>
using namespace std;
int n, m, ans((99999999));
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  bool flag(false);
  for (int i = 0; 2 * i <= n; i++) {
    if ((n - i) % m == 0) {
      if (n - i < ans) {
        ans = n - i;
      }
    }
  }
  if (ans == (99999999))
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
