#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using ull = unsigned long long;

template<typename T>
using vec = vector<T>;
template<typename T>
using mat = vector<vec<T>>;
template<typename T>
using val = valarray<T>;

using pi = pair<int,int>;

#define rep(a,b) for(int a=0; a<b; ++a)
#define Rep(a,b,c) for(int a=b; a<c; ++a)
#define ran(a,b) for(auto &a:b)
#define all(v) v.begin(),v.end()

string smax(string a,string b){
	if(a.size()!=b.size()){
		return a.size()>b.size()?a:b;
	}
	rep(i,a.size()){
		if(a[i]!=b[i]){
			return a[i]>b[i]?a:b;
		}
	}
	return a;
}

string smin(string a,string b){
	if(a.size()!=b.size()){
		return a.size()<b.size()?a:b;
	}
	rep(i,a.size()){
		if(a[i]!=b[i]){
			return a[i]<b[i]?a:b;
		}
	}
	return a;
}

int ssub(string a,string b){
	//cout<<a<<" "<<b<<endl;
	int pnt;
	rep(i,a.size()){
		if(a[i]!=b[i]){
			pnt=i;
			break;
		}
	}
	if(a.size()-pnt-1>=2) return 37564;
	else{
		if(a.size()==1){
			return stoi(a)-stoi(b);
		}
		else{
			return stoi(a.substr(a.size()-2,2))-stoi(b.substr(b.size()-2,2));
		}
	}
}

int main(){
	string n; cin >> n;
	
	int ans = 10;
	
	string max1,min1;
	Rep(i,1,n.size()){
		//cout<<"Check"<<endl;
		if(n.size()%i!=0) continue;
		max1="0";
		min1=n;
		//cout<<"Check"<<endl;
		for(int j=0; j<n.size(); j+=i){
			string tmp=n.substr(j,i);
			//cout<<"Check"<<endl;
			max1=smax(max1,tmp);
			min1=smin(min1,tmp);
		}
		//cout<<"Case "<<i<<" : "<<max1<<" "<<min1<<endl;
		ans=min(ans,ssub(max1,min1));
		//cout<<"Check"<<endl;
	}
	//cout<<"Check"<<endl;
	
	int max2=0,min2=10;
	int pnt=0;
	while(pnt<n.size()){
		int tmp=n[pnt]-'0';
		if(n[pnt]=='1'&&pnt+1<n.size()){
			tmp*=10;
			tmp+=(n[pnt+1]-'0');
			++pnt;
		}
		++pnt;
		max2=max(max2,tmp);
		min2=min(min2,tmp);
	}
	//cout<<max2<<" "<<min2<<endl;
	ans=min(ans,max2-min2);
	cout<<ans<<endl;
}
