program quicksumm;
const maxn=500001;
var s:string;
n,l1,l2,ans:longint;
step,now:array[0..maxn-1]of longint;
que:array[0..maxn-1]of string;
function delete(s:string;p:longint):string;
var i,j:longint;
begin
delete:='';
j:=length(s);
p:=p+1;
while (s[p]='0')and(p<=j) do inc(p);
for i:=p to j do
delete:=delete+s[i];
exit(delete);
end;
function copy(s:string;p:longint):longint;
var i,j,k:longint;
begin
j:=0;
k:=1;
while s[k]='0' do
begin
s:=delete(s,1);
dec(p);
end;
for i:=1 to p do
begin
j:=j*10;
j:=j+ord(s[i])-ord('0');
end;
exit(j);
end;
function val(s:string):longint;
var i,j,k:longint;
begin
k:=length(s);
if k>5 then exit(-100000);
j:=0;
for i:=1 to k do
begin
j:=j*10;
j:=j+ord(s[i])-ord('0');
end;
exit(j);
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a);
exit(b);
end;
function bfs:boolean;
var l,r,v,w,i,j:longint;
u:string;
begin
fillchar(que,sizeof(que),0);
fillchar(step,sizeof(step),0);
fillchar(now,sizeof(now),0);
l:=0;r:=1;
que[0]:=s;
step[0]:=0;
now[0]:=0;
while l<>r do
begin
u:=que[l];
j:=length(u);
for i:=1 to min(l1,j-1) do
if u[i]<>'0' then
begin
w:=now[l]+copy(u,i);
if w>n then break;
que[r]:=delete(u,i);
step[r]:=step[l]+1;
now[r]:=w;
w:=val(que[r]);
if now[r]+w=n then
begin
ans:=step[r];
exit(true);
end;
r:=(r+1)mod maxn;
end;
l:=(l+1)mod maxn;
end;
exit(false);
end;
procedure init;
var i,j,k:longint;
ch:char;
begin
assign(input,'quicksum3.in');
reset(input);
assign(output,'quicksum.out');
rewrite(output);
s:=''; l1:=0;
read(ch);
while (ord(ch)>=ord('0'))and(ord(ch)<=ord('9'))do
begin
s:=s+ch;
read(ch);
inc(l1);
end;
k:=1;
while s[k]='0' do
delete(s,1);
read(n);
k:=n;l2:=0;
while k<>0 do
begin
inc(l2);
k:=k div 10;
end;
end;
procedure main;
begin
if l1<l2 then begin write(-1);exit;end;
ans:=0;
if bfs then write(ans)
else write(-1);
end;
begin
init;
main;
end.
评论