​http://acm.hdu.edu.cn/showproblem.php?pid=2089​

 

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

int dp[20][2];
int bit[20];

int dfs(int pos,int pre,int sta,int limit)
{
int res,up,i;
if(pos==-1) return 1;
if(!limit&&dp[pos][sta]!=-1) return dp[pos][sta];
if(limit) up=bit[pos];
else up=9;
res=0;
for(i=0;i<=up;i++){
if((pre==6&&i==2)||i==4) continue;
res+=dfs(pos-1,i,i==6,limit&&i==bit[pos]);
}
if(!limit) dp[pos][sta]=res;
return res;
}

int solve(int val)
{
int n;
if(val==-1) return 0;
n=0;
while(val>0){
bit[n++]=val%10;
val/=10;
}
return dfs(n-1,-1,0,1);
}

int main()
{
int l,r;
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&l,&r)!=EOF){
if(l==0&&r==0) break;
printf("%d\n",solve(r)-solve(l-1));
}
return 0;
}