#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

map<int, int> mp;
string str;
int n;
int solve(string ss, int cnt){
	int mn = INF, mx = -1, m = ss.size();
	string s;
	rrep(i,m){
		s.push_back(ss[i-1]);
		if(i%cnt == 0){
			int num = atoll(s.c_str());
			//cout << s << " " << cnt << endl;
			chmax(mx, num);
			chmin(mn, num);
			s.clear();
			//printf("%lld %lld\n", num, cnt);
		}
	}
	//printf("%lld %lld %lld\n", mx, mn, cnt);
	return mx-mn;
}

int solve1(int cnt){
	string s = str;
	if(cnt > 4){
		s.clear();
		s = str.substr(0, cnt-3);
		rep(i,n/cnt)if(str.substr(i*cnt, cnt-3) != s){return INF;}
		s.clear();
		rep(i,n)if(i%cnt >= cnt-3)s += str[i];
		//cout << s << endl;
		cnt = 3;
	}
	return solve(s, cnt);
}

int solve2(){
	int mx = -1, mn = INF;
	string s;
	rep(i,n){
		s.pb(str[i]);
		if(str[i]-'0' != 1 or s.size() == 2){
			int num = atoll(s.c_str());
			chmax(mx, num);
			chmin(mn, num);
			s.clear();
		}
	}
	if(s.size() > 0){
		int num = atoll(s.c_str());
		chmax(mx, num);chmin(mn, num);
	}
	return mx-mn;
}

signed main(){
	cin >> str;
	n = str.size();
	for(int i = 1;i < n;i++){
		if(n%i == 0){mp[i] = 1;}
	}
	int ans = INF;
	for(map<int, int>::iterator it = mp.begin();it != mp.end();it++){
		chmin(ans, solve1(it->fi));
	}
	if(n != 2)chmin(ans, solve2());
	printf("%lld\n", ans);
	return 0;
}






