1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using namespace std;
#include <iostream>
#include <unordered_map>
#include <vector>

unordered_map<long long,bool> tab3, tab4, tab7, tab8, tab9;

void dzielniki(long long &a, long long &b)
{
    long long l = a%3;
    for(long long j=a+(l!=0 ? 3-l : 0); j<=b; j+=3)
    {
        tab3[j]=1;
    }

    l=a%4;

    for(long long j=a+(l!=0 ? 4-l : 0); j<=b; j+=4)
    {
        tab4[j]=1;
    }

    l=a%7;

    for(long long j=a+(l!=0 ? 7-l : 0); j<=b; j+=7)
    {
        tab7[j]=1;
    }

    l=a%8;

    for(long long j=a+(l!=0 ? 8-l : 0); j<=b; j+=8)
    {
        tab8[j]=1;
    }

    l=a%9;

    for(long long j=a+(l!=0 ? 9-l : 0); j<=b; j+=9)
    {
        tab9[j]=1;
    }

}

bool czyPotyczkowa(long long& z)
{
    bool dzielniki[10] = {false};

    long long x = z;

    while(x>0)
    {
        
        long long y = x%10;
        dzielniki[y] = true;

        if(dzielniki[0] == true)
        {
            return false;
        }

        x/=10;
    }

    if(dzielniki[2] == true)
    {
        if( z % 2 != 0)
            return false;

        if(dzielniki[6] == true)
        {
            if(tab3[z] != true)
                return false;
        }

    }
    else if(dzielniki[6] == true)
    {
        if( z % 2 != 0 || tab3[z] != true)
            return false;
    }

    if(dzielniki[3] == true)
    {
        if( tab3[z] != true)
            return false;
    }

    if(dzielniki[4] == true)
    {
        if( tab4[z] != true)
            return false;
    }

    if(dzielniki[5] == true)
    {
        if( z%10 != 5)
            return false;
    }

    if(dzielniki[7] == true)
    {
        if( tab7[z] != true)
            return false;
    }

    if(dzielniki[8] == true)
    {
        if( tab8[z] != true)
            return false;
    }

    if(dzielniki[9] == true)
    {
        if( tab9[z] != true)
            return false;
    }

    return true;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    long long l, r;
    cin>>l>>r;
    dzielniki(l, r);
    long long c = 0;
    for(long long i=l; i<=r; i++)
    {
        if( czyPotyczkowa(i) )
        {
            c++;
        }
    }
    
    cout<<c;
    
    return 0;
}