# -2 Calculating # -1 Not yet calculated # 0 Happy # 1 Unhappy # The sum of the squares of an all-9, 20-digits number U = [-1] * (9 ** 2 * 20 + 1) U[0],U[1]=0,0 def calcU(n): if U[n] >= 0: return U[n] if U[n] == -2: U[n] = 1 return 1 U[n] = -2 n_,s_=n,0 while n_: s_+=(n_%10)*(n_%10) n_/=10 U[n] = calcU(s_) return U[n] # DP[PartialSumOfSquareDigits][RemainingDigits] -> How many unhappy numbers can be generated? DP = [[-1 for e in range(20+1)] for e in range(9 ** 2 * 20 + 1)] def calcDP(ps, rd): if DP[ps][rd] != -1: return DP[ps][rd] if rd == 0: DP[ps][0] = calcU(ps) return DP[ps][0] DP[ps][rd] = 0 for i in range(10): DP[ps][rd] += calcDP(ps + i*i, rd - 1) return DP[ps][rd] # How many unhappy numbers can be generated? # Example: 318 # 0XY -> calcDP(0*0, 2) # 1XY -> calcDP(1*1, 2) # 2XY -> calcDP(2*2, 2) # 30X -> calcDP(3*3+0*0,1) # 310 -> calcDP(3*3+1*1+0*0,0) -> calcU(3*3+1*1+0*0) # 311 -> calcDP(3*3+1*1+1*1,0) -> calcU(3*3+1*1+1*1) # 312 -> calcDP(3*3+1*1+2*2,0) -> calcU(3*3+1*1+2*2) #... # 318 -> calcDP(3*3+1*1+8*8,0) -> calcU(3*3+1*1+8*8) def calc(D, pos, ps, rd): if rd == 0: return calcDP(ps, 0) res = calc(D, pos + 1, ps + D[pos]*D[pos], rd - 1) for i in range(D[pos]): res += calcDP(ps + i*i, rd - 1) return res if __name__ == "__main__": while True: start, end = [int(e) for e in raw_input().split()] if start == 0 and end == 0: break start -= 1 S = [] while start: S=[start%10]+S start/=10 E = [] while end: E=[end%10]+E end/=10 print calc(E,0,0,len(E))-calc(S,0,0,len(S))