#include "bits/stdc++.h" using namespace std; int x,y,z,target; struct state{ int x,y,z,step; }f; queue<state> q; bool vis[200+10][200+10][200+10]; int bfs() { q.push(f); vis[f.x][f.y][f.z]=1; while(q.size()) { f=q.front(); q.pop(); if(f.x==target||f.y==target||f.z==target) return f.step; if(f.x<x&&vis[x][f.y][f.z]==0) //x倒满 { q.push((state){x,f.y,f.z,f.step+1}); vis[x][f.y][f.z]=1; } if(f.x&&vis[0][f.y][f.z]==0) //x倒空 { q.push((state){0,f.y,f.z,f.step+1}); vis[0][f.y][f.z]=1; } if(f.y<y&&vis[f.x][y][f.z]==0) //y倒满 { q.push((state){f.x,y,f.z,f.step+1}); vis[f.x][y][f.z]=1; } if(f.y&&vis[f.x][0][f.z]==0) //y倒空 { q.push((state){f.x,0,f.z,f.step+1}); vis[f.x][0][f.z]=1; } if(f.z<z&&vis[f.x][f.y][z]==0) //z倒满 { q.push((state){f.x,f.y,z,f.step+1}); vis[f.x][f.y][z]=1; } if(f.z&&vis[f.x][f.y][0]==0) //z倒空 { q.push((state){f.x,f.y,0,f.step+1}); vis[f.x][f.y][0]=1; } if(f.x>=y-f.y&&vis[f.x-(y-f.y)][y][f.z]==0)//x->y { q.push((state){f.x-(y-f.y),y,f.z,f.step+1}); vis[f.x-(y-f.y)][y][f.z]=1; } if(f.x<y-f.y&&vis[0][f.x+f.y][f.z]==0)//x->y { q.push((state){0,f.x+f.y,f.z,f.step+1}); vis[0][f.x+f.y][f.z]=1; } if(f.y>=x-f.x&&vis[x][f.y-(x-f.x)][f.z]==0)//y->x { q.push((state){x,f.y-(x-f.x),f.z,f.step+1}); vis[x][f.y-(x-f.x)][f.z]=1; } if(f.y<x-f.x&&vis[f.x+f.y][0][f.z]==0)//y->x { q.push((state){f.x+f.y,0,f.z,f.step+1}); vis[f.x+f.y][0][f.z]=1; } if(f.x>=z-f.z&&vis[f.x-(z-f.z)][f.y][z]==0)//x->z { q.push((state){f.x-(z-f.z),f.y,z,f.step+1}); vis[f.x-(z-f.z)][f.y][z]=1; } if(f.x<z-f.z&&vis[0][f.y][f.x+f.z]==0)//x->z { q.push((state){0,f.y,f.x+f.z,f.step+1}); vis[0][f.y][f.x+f.z]=1; } if(f.y>=z-f.z&&vis[f.x][f.y-(z-f.z)][z]==0)//y->z { q.push((state){f.x,f.y-(z-f.z),z,f.step+1}); vis[f.x][f.y-(z-f.z)][z]=1; } if(f.y<z-f.z&&vis[f.x][0][f.y+f.z]==0)//y->z { q.push((state){f.x,0,f.y+f.z,f.step+1}); vis[f.x][0][f.y+f.z]=1; } if(f.z>=x-f.x&&vis[x][f.y][f.z-(x-f.x)]==0)//z->x { q.push((state){x,f.y,f.z-(x-f.x),f.step+1}); vis[x][f.y][f.z-(x-f.x)]=1; } if(f.z<x-f.x&&vis[f.x+f.z][f.y][0]==0)//z->x { q.push((state){f.x+f.z,f.y,0,f.step+1}); vis[f.x+f.z][f.y][0]=1; } if(f.z>=y-f.y&&vis[f.x][y][f.z-(y-f.y)]==0)//z->y { q.push((state){f.x,y,z-(y-f.y),f.step+1}); vis[f.x][y][z-(y-f.y)]=1; } if(f.z<y-f.y&&vis[f.x][f.y+f.z][0]==0)//y->z { q.push((state){f.x,f.y+f.z,0,f.step+1}); vis[f.x][f.y+f.z][0]=1; } } return -1; } int main() { scanf("%d%d%d%d",&x,&y,&z,&target); int ans=bfs(); cout<<ans<<endl; return 0; }