#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define put(x) cout << x << endl;
using namespace std;
int main()
{
    ll x;
    cin >> x;
    put(1000 * (x / 500) + 5 * ((x % 500) / 5))
    return 0;
}
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using ll=unsigned long long;
using namespace std;

int main() {
    string s;
    cin>>s;
    int minv=1000000005;
    size_t l=s.length();
    rep(i,l-2){
        string ss=s.substr(i,3);
        int num=stoi(ss);
        minv=min(minv,abs(num-753));
    }
    cout<<minv<<"\n";
    return 0;
}