using
System;
using
System.Collections.Generic;
public
class
pair : IComparable<pair> {
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
public
int
CompareTo(pair ob)
{
if
(
this
.first != ob.first) {
return
this
.first - ob.first;
}
return
this
.second - ob.second;
}
public
override
string
ToString()
{
return
first +
" "
+ second;
}
static
public
pair
from
(
int
f,
int
s)
{
return
new
pair(f, s);
}
}
public
class
GFG {
int
C, D;
Dictionary<pair,
int
> dp;
const
int
oo = (
int
)1e8;
bool
fits(
int
a,
int
b,
int
x,
int
y)
{
if
(a <= x && b <= y) {
return
true
;
}
int
tmp = a;
a = b;
b = tmp;
if
(a <= x && b <= y) {
return
true
;
}
return
false
;
}
int
solve(
int
A,
int
B)
{
if
(fits(A, B, C, D)) {
return
0;
}
if
(dp.ContainsKey(pair.
from
(A, B))) {
return
dp[pair.
from
(A, B)];
}
int
op1 = oo, op2 = oo;
if
(A != 0) {
op1 = 1 + solve(A / 2, B);
}
if
(B != 0) {
op2 = 1 + solve(A, B / 2);
}
int
ret = Math.Min(op1, op2);
dp[pair.
from
(A, B)] = ret;
return
ret;
}
int
carpetBox(
int
A,
int
B,
int
C,
int
D)
{
this
.C = C;
this
.D = D;
dp =
new
Dictionary<pair,
int
>();
int
result = solve(A, B);
return
result == oo ? -1 : result;
}
static
public
void
Main()
{
GFG s =
new
GFG();
int
A = 8, B = 13, C = 6, D = 10;
int
result = s.carpetBox(A, B, C, D);
Console.WriteLine(result);
}
}