#include<bits/stdc++.h> using namespace std; int main(){ int p,e,i,d; cin>>p>>e>>i>>d; for(int j=1;j>=0;j++){ d=d+1; if(d%(23-p)==d%(28-e)&&d%(28-e)==d%(33-i)){ cout<<d; return 0; } } }