#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, ans = 0, ons = 0, twos = 2;
  cin >> n >> m;
  if (n < m) {
    cout << "-1" << endl;
    return 0;
  } else {
    if (n % 2 == 0)
      twos = n / 2, ons = 0;
    else
      twos = (n - 1) / 2, ons = 1;
    while (1) {
      ans = twos + (ons);
      if (ans % m == 0) {
        cout << ans << endl;
        return 0;
      }
      if (twos == 0) break;
      twos--;
      ons += 2;
    }
    cout << "-1" << endl;
  }
}
