import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto AB = readln.split.to!(ulong[]); auto A = AB[0]; auto B = AB[1]; long solve() { if (A == 1 || B == 1) return 0; if (A % 2 == 0 && B % 2 == 0) return -1; bool[ulong] stock; auto smaller = A < B ? A : B; auto larger = A < B ? B : A; auto step = larger - smaller; foreach(n; 1..101) { foreach(i; iota(smaller*n, larger*n+1, step)) { stock[i] = true; } int continuas; foreach(i; smaller*(n-1)..larger*n) { if (!(i in stock)) { continuas = 0; continue; } if (++continuas == smaller) { int answer; foreach(j; 1..i) if (!(j in stock)) answer++; return answer; } } } return -1; } solve().writeln; }