N = gets.chomp

def ok(d)
  1.upto(N.size/2) do |i|
    min = max = N[0...i].to_i
    l = i
    while l < N.size
      ok = false
      [l+i-1, l+i, l+i+1].each do |r|
        next unless l < r
        next unless r <= N.size
        t = N[l...r].to_i   #
        next unless [max, t].max - [min, t].min <= d
        min = [min, t].min
        max = [max, t].max
        l = r
        ok = true
        break
      end
      break unless ok
    end
    return true if l == N.size
  end
  false
end

p (0..8).find{|i| ok(i)}