#include <iostream>
#include <array>
#include <vector>
#include <queue>
using namespace std;
struct VolumeState {
array<int, 4> V{};
int Step;
VolumeState() {
V = {0, 0, 0, 0};
Step = 0;
}
};
void Pour(int i, int j, VolumeState &s, const array<int, 4> &Volume) {
int Sum = s.V[i] + s.V[j];
if(Sum > Volume[j]) {
s.V[i] = Sum - Volume[j];
s.V[j] = Volume[j];
}
else {
s.V[i] = 0;
s.V[j] = Sum;
}
}
int BFS(VolumeState start, int t, const array<int, 4> &Volume, vector<vector<vector<bool>>> &Visited) {
queue<VolumeState> Queue;
Queue.push(start);
while (!Queue.empty()) {
VolumeState state = Queue.front();
Queue.pop();
Visited[state.V[1]][state.V[2]][state.V[3]] = 1;
for (int i = 1; i <= 3; ++i) {
if (state.V[i] == 0)
continue;
for (int j = 1; j <= 3; ++j) {
if (i != j) {
VolumeState temp = state;
Pour(i, j, temp, Volume);
temp.Step++;
if(temp.V[1] == t || temp.V[2] == t || temp.V[3] == t)
return temp.Step;
if(!Visited[temp.V[1]][temp.V[2]][temp.V[3]])
Queue.push(temp);
}
}
}
}
return INT_MAX;
}
int main() {
int x, y, z, t;
cin >> x >> y >> z >> t;
array<int, 4> Volume{0, x, y, z};
vector<vector<vector<bool>>> Visited(x + 1, vector<vector<bool>>(y + 1, vector<bool>(z + 1, 0)));
if (t == x || t == y || t == z) {
cout << 0 << endl;
return 0;
}
int MinStep = INT_MAX;
for (int i = 1; i <= 3; ++i) {
VolumeState start;
start.V[i] = Volume[i];
MinStep = min(MinStep, BFS(start, t, Volume, Visited));
for (int j = 1; j <= 3; ++j) {
if (i != j) {
start.V[j] = Volume[j];
MinStep = min(MinStep, BFS(start, t, Volume, Visited));
}
}
}
if (MinStep == INT_MAX)
cout << -1 << endl;
else
cout << MinStep << endl;
return 0;
}