#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (m > n)
    printf("-1\n");
  else {
    int x = n / 2;
    int y = n % 2;
    while ((x + y) % m != 0) {
      x--;
      y += 2;
      if (x < 0) {
        printf("-1\n");
        return 0;
      }
    }
    printf("%d", x + y);
  }
}
