program MayLava;
var
ss:array[1..4000000]of boolean;
a,b,sum:longint;
d:char;
procedure start;
var
f:text;
begin
assign(f,'qprime.in');
reset(f);
readln(f,a,b,d,d);
close(f);
sum:=0;
fillchar(ss,sizeof(ss),true);
end;
procedure creative;
var
i,j:longint;
begin
ss[1]:=false;
for i:=2 to (b div 2) do begin
if not ss[i] then continue;
j:=2;
repeat
ss[i*j]:=false;
inc(j);
until i*j>b;
end;
end;
procedure main;
var
i:longint;
s:string;
begin
for i:=a to b do begin
if not ss[i] then continue;
str(i,s);
if pos(d,s)<>0 then inc(sum);
end;
end;
procedure print;
var
f:text;
begin
assign(f,'qprime.out');
rewrite(f);
writeln(f,sum);
close(f);
end;
BEGIN
start;
creative;
main;
print;
END.