function e=min_e(Sp, T, p_min)
N=size(Sp, 1);
m=1;
for i=1:N
    Sx=Sp(i,1);
    Sy=Sp(i,2);
    D=Diff_parametrine(Sx, Sy);
    D2=diff(D);
    for j=T(i):0.001:T(i+1)
        t=j;
        kreivis(m)=eval(D2)/(1+(eval(D))^2)^(3/2);
        x=eval(Sp(i,1));
        y=eval(Sp(i,2));
        kreive(m, :)=[i j x y];
        spind(m)=1/kreivis(m);
        m=m+1;
    end
end
% [max_kreivis i]=max(abs(kreivis));
 [max_kreivis i]=min(abs(spind));
 kreiv=mean(abs(kreivis));
i=72;
%surastas taskas funkcijoje, kuriame kreives kreivis yra didziausias
fx=Sp(kreive(i,1),1);
fy=Sp(kreive(i,1),2);
t=kreive(i,2);
xf=eval(fx);
yf=eval(fy);
funkcija=kreive(i,1);

syms x1 y1 xg yg tg l t1 t2 x2 y2
D=Diff_parametrine(fx, fy);
t=kreive(i,2);
dd=eval(D);
if dd~=0
   f2=-1/dd*(xg-xf)-(yg-yf);
else
    yg=yf;
    f2=(xg-xf)*tg+xf;
end
f1=dd*(xg-x1)-(yg-y1);
f3=(x1-xg)^2+(y1-yg)^2-(p_min/2)^2; 
f4=fx-x1;
f5=fy-y1;
f1=dd*(x2-x1)-(y2-y1);
f2=(x2-x1)^2+(y2-y1)^2-(p_min*1.1)^2;
t=t1;
funx=Sp(kreive(i,1),1);
funy=Sp(kreive(i,1),2);
f3=eval(funx)-x1;
f4=eval(funy)-y1;
t=t2;
funx=Sp(kreive(i,1),1);
funy=Sp(kreive(i,1),2);
f5=eval(funx)-x2;
f6=eval(funy)-y2;
% f9=((xg-x1)^2+(yg-y1)^2)/((xg-x2)^2+(yg-y2)^2)-l^2;
a=solve(f1,f2,f3,f4,f5,f6);
tt1=double(a.t1(3,1));
tt2=double(a.t2(3,1));
xx1=double(a.x1(1,1));
yy1=double(a.y1(1,1));
xx2=double(a.x2(1,1));
yy2=double(a.y2(1,1));

if dd~=0
   f1=-1/dd*(xg-xf)-(yg-yf);
else
    yg=yf;
    f1=(xg-xf)*tg+xf;
end
f2=((xg-xx1)/(xx2-xx1))-((yg-yy1)/(yy2-yy1));
a=solve(f1,f2);
%---------
xg=double(a.xg);
% tt=double(a.t);
if dd~=0
    yg=double(a.yg);
end

kreiv=mean(kreivis);
kreiv_std=std(kreivis);
santykis=kreiv/kreiv_std;
e=atstumas(xf,xg, yf, yg);
