#include <bits/stdc++.h>
using namespace std;
struct State {
  bool side;
  int type, GL, WL, GR, WR;
  bool operator<(const State &other) const {
    if (side != other.side) return side < other.side;
    if (type != other.type) return type < other.type;
    if (GL != other.GL) return GL < other.GL;
    return WL < other.WL;
  }
  void print() {
    printf("%s %d %d %d %d %d\n", side ? "right" : "left", type, GL, WL, GR,
           WR);
  }
};
int m, n;
queue<State> Q;
set<State> Rem;
map<State, int> dist;
void process(State &u, State &v, State &w) {
  while (true) {
    auto it = Rem.lower_bound(v);
    if (it == Rem.end() or w < *it) break;
    dist[*it] = dist[u] + 1;
    Q.push(*it);
    Rem.erase(it);
  }
}
void process01(State &u) {
  {
    int a = u.GL, b = u.GR, x0 = 1, x1 = min(n / 2, a - 1);
    State v = {true, 1, a - x1, a - x1, b + x1, b + x1},
          w = {true, 1, a - x0, a - x0, b + x0, b + x0};
    process(u, v, w);
  }
  {
    int a = u.GL, b = u.GR, x0 = 0, x1 = min(a, n - a);
    State v = {true, 2, 0, a - x1, m, b + x1},
          w = {true, 2, 0, a - x0, m, b + x0};
    process(u, v, w);
  }
  {}
}
void process02(State &u) {
  {} {
    int a = u.WL, b = u.WR, x0 = 1, x1 = min(n, a);
    State v = {true, 2, 0, a - x1, m, b + x1},
          w = {true, 2, 0, a - x0, m, b + x0};
    process(u, v, w);
  }
  {}
}
void process03(State &u) {
  {
    int a = u.WL, b = u.WR, x0 = max(1, b),
        x1 = min({m - 1, a + b, (n + b) / 2}), y0 = x0 - b, y1 = x1 - b;
    State v = {true, 1, m - x1, a - y1, x1, b + y1},
          w = {true, 1, m - x0, a - y0, x0, b + y0};
    process(u, v, w);
  }
  {
    int a = u.WL, b = u.WR, x0 = 0, x1 = min(a, n - m);
    State v = {true, 2, 0, a - x1, m, b + x1},
          w = {true, 2, 0, a - x0, m, b + x0};
    process(u, v, w);
  }
  {
    int a = u.WL, b = u.WR, x0 = 1, x1 = min(a, n);
    State v = {true, 3, m, a - x1, 0, b + x1},
          w = {true, 3, m, a - x0, 0, b + x0};
    process(u, v, w);
  }
}
void process11(State &u) {
  {
    int a = u.GL, b = u.GR, x0 = 1, x1 = min(n / 2, b - 1);
    State v = {false, 1, a + x0, a + x0, b - x0, b - x0},
          w = {false, 1, a + x1, a + x1, b - x1, b - x1};
    process(u, v, w);
  }
  {} {
    int a = u.GL, b = u.GR, x0 = 0, x1 = min(b, n - b);
    State v = {false, 3, m, a + x0, 0, b - x0},
          w = {false, 3, m, a + x1, 0, b - x1};
    process(u, v, w);
  }
}
void process12(State &u) {
  {
    int a = u.WL, b = u.WR, x0 = max(1, a),
        x1 = min({m - 1, a + b, (n + a) / 2}), y0 = x0 - 1, y1 = x1 - a;
    State v = {false, 1, x0, m - x0, a + y0, b - y0},
          w = {false, 1, x1, m - x1, a + y1, b - y1};
    process(u, v, w);
  }
  {
    int a = u.WL, b = u.WR, x0 = 1, x1 = min(b, n);
    State v = {false, 2, 0, a + x0, m, b - x0},
          w = {false, 2, 0, a + x1, m, b - x1};
    process(u, v, w);
  }
  {
    int a = u.WL, b = u.WR, x0 = 0, x1 = min(n - m, b);
    State v = {false, 3, m, a + x0, 0, b - x0},
          w = {false, 3, m, a + x1, 0, b - x1};
    process(u, v, w);
  }
}
void process13(State &u) {
  {} {} {
    int a = u.WL, b = u.WR, x0 = 1, x1 = min(n, b);
    State v = {false, 3, m, a + x0, 0, b - x0},
          w = {false, 3, m, a + x1, 0, b - x1};
    process(u, v, w);
  }
}
void bfs() {
  State u = {false, 3, m, m, 0, 0};
  dist[u] = 0;
  Q.push(u);
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    if (!u.side) {
      if (u.type == 1)
        process01(u);
      else if (u.type == 2)
        process02(u);
      else
        process03(u);
    } else {
      if (u.type == 1)
        process11(u);
      else if (u.type == 2)
        process12(u);
      else
        process13(u);
    }
  }
}
int main() {
  scanf("%d %d", &m, &n);
  for (int i = 0; i <= m; i++) {
    if (i and i < m) Rem.insert({false, 1, i, i, m - i, m - i});
    if (i and i < m) Rem.insert({true, 1, i, i, m - i, m - i});
    Rem.insert({false, 2, 0, i, m, m - i});
    Rem.insert({true, 2, 0, i, m, m - i});
    Rem.insert({false, 3, m, i, 0, m - i});
    Rem.insert({true, 3, m, i, 0, m - i});
  }
  bfs();
  State target = {true, 2, 0, 0, m, m};
  printf("%d\n", dist.count(target) ? dist[target] : -1);
  return 0;
}
