#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, kecil, tamp1 = 0, tamp2 = 0, tambah = 0;
  cin >> n;
  cin >> m;
  tamp1 = n;
  tamp2 = m;
  while (n != 0) {
    n = tamp1;
    if (tambah >= 1) m += tamp2;
    if (m > n) {
      cout << -1;
      return 0;
    }
    for (int i = 0; i < m; i++) {
      if (n == m - i)
        n -= 1;
      else
        n -= 2;
    }
    tambah++;
  }
  cout << m;
}
