#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  if (n < m)
    cout << -1;
  else if (n == 0)
    cout << 0;
  else if (n == 1)
    if (m == 1)
      cout << 1;
    else
      cout << -1;
  else {
    vector<int> v;
    while (n > 0) {
      if (n > 1) {
        v.push_back(2);
        n -= 2;
      } else {
        v.push_back(1);
        n -= 1;
      }
    }
    if (v.size() % m == 0)
      cout << v.size();
    else {
      int i = 0, c = v.size();
      while (v[i++] == 2) {
        c++;
        if (c % m == 0) {
          cout << c;
          return 0;
        }
      }
      cout << -1;
    }
  }
  return 0;
}
