local n = io.read("*n")
local dignum = 0
local digits = {}
while 0 < n do
  table.insert(digits, n % 10)
  n = n // 10
  dignum = dignum + 1
end
local norm = {}
local maymax = {}

-- 0, 03, 05, 07, 035, 037, 057, 0357
for i = 1, 8 do
  norm[i] = {}
  maymax[i] = {}
  for j = 1, dignum do
    norm[i][j] = 0
    maymax[i][j] = 0
  end
end
local digval = digits[dignum]
norm[1][dignum] = 1
if 8 <= digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  norm[4][dignum] = 1
elseif 7 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  maymax[4][dignum] = 1
elseif 6 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
elseif 5 == digval then
  norm[2][dignum] = 1
  maymax[3][dignum] = 1
elseif 4 == digval then
  norm[2][dignum] = 1
elseif 3 == digval then
  maymax[2][dignum] = 1
end

for i = dignum - 1, 1, -1 do
  local digval = digits[i]
  norm[1][i] = 1
  -- 03, 05, 07
  for j = 2, 4 do
    local tgt = j * 2 - 1
    if tgt < digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif tgt == digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = maymax[j][i + 1]
    else
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = 0
    end
  end
  -- 035, 037, 057
  for j = 5, 7 do
    local rightval = j == 5 and 5 or 7
    local rightidx = j == 5 and 3 or 4
    local leftval = j == 7 and 5 or 3
    local leftidx = j == 7 and 3 or 2
    if rightval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[leftidx][i + 1] + maymax[rightidx][i + 1] + maymax[j][i + 1] * 2
      maymax[j][i] = 0
    elseif rightval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = maymax[leftidx][i + 1] + maymax[j][i + 1]
    elseif leftval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif leftval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = maymax[rightidx][i + 1] + maymax[j][i + 1]
    else
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = 0
    end
  end
  if 7 < digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[5][i + 1] + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 3
    maymax[8][i] = 0
  elseif 7 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = maymax[5][i + 1] + maymax[8][i + 1]
  elseif 6 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = 0
  elseif 5 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = maymax[6][i + 1] + maymax[8][i + 1]
  elseif 4 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = 0
  elseif 3 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = maymax[7][i + 1] + maymax[8][i + 1]
  else
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = 0
  end
end
print(norm[8][1] + maymax[8][1])


Measured memory_bytes: 888