Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he received embosser and was to take stock of the whole exposition.Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:  After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Note   To print the string from the first sample it would be optimal to perform the following sequence of rotations:   from 'a' to 'z' (1 rotation counterclockwise),  from 'z' to 'e' (5 clockwise rotations),  from 'e' to 'u' (10 rotations counterclockwise),  from 'u' to 's' (2 counterclockwise rotations).  In total, 1 + 5 + 10 + 2 = 18 rotations are required.

uses math;
var s:string; i,sum:longint; last:char;
function a(s:longint):longint;
begin
  if s<0 then exit(min(-s,26+s));
  exit(min(s,26-s));
end;
begin
  read(s);
  last:='a';
  for i:=1 to length(s) do
  begin
    sum:=sum+a(ord(last)-ord(s[i]));
    last:=s[i];
  end;
  writeln(sum);
end.

program O_O;
uses math;
var
	i,n,ans:longint;
	s:string;
begin
	readln(s);
	s:='a'+s;
	for i:=1 to length(s)-1 do
	begin
		inc(ans,min(abs(ord(s[i+1])-ord(s[i])),26-abs(ord(s[i+1])-ord(s[i]))));
	end;
	writeln(ans);
end.

var
p,ans,d,q,f,i,j:longint;
a:Array[1..100000]of longint;

s:string;
begin
readln(s);
for i:=1 to 26 do begin
    a[i+96]:=i;
end;
p:=1;
for i:=1 to length(s) do begin
    d:=abs(a[ord(s[i])]-p);
    f:=26-abs((a[ord(s[i])]-p));
    if d<f then begin
        ans:=ans+d;
    end
    else begin
    ans:=ans+f;
    end;
    p:=a[ord(s[i])];
end;
writeln(Ans);
end.

var
s:string;
ind1,min,ans,kol,ind,i:longint;
begin
readln(s);
ind:=97;
for i:=1 to length(s)do begin
kol:=0;
ind1:=ind;
    while true do begin
        if char(ind1)=s[i] then break
        else begin
            inc(ind1);
            inc(kol);
            if ind1>122 then ind1:=97;
        end;
    end;
    min:=kol;
    kol:=0;
    ind1:=ind;
    while true do begin
        if char(ind1)=s[i] then break
        else begin
            dec(ind1);
            inc(kol);
            if ind1<97 then ind1:=122;
        end;
    end;
    if kol<min then min:=kol;
    ans:=ans+min;
    ind:=ord(s[i]);
end;
writeln(ans);
end.

program Codefroce;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  s: string;
  i, ans, st: integer;

begin
  readln(s);
  ans := 0;
  i := 0;
  while i < length(s) do
  begin
    if (i = 0) then st := 1
      else st := ord(s[i]) - 96;
    ans := ans + min(abs(st - (ord(s[i + 1]) - 96)), abs(26 - abs(st - (ord(s[i + 1]) - 96))));
    inc(i);
  end;
  writeln(ans);
  readln;

end.

program zada;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
    s: string;
    i, k, a, x: integer;

begin
    readln(s);
    a := 97;
    k := 0;

    for i := 1 to length(s) do
        begin
            x := abs(ord(s[i]) - a);
            if (x > 13) then
                k := k + 25 - x + 1
            else
                k := k + x;
            a := ord(s[i]);
        end;

    writeln(k);
    readln;
end.

 var p,s:string;
    k,z,t,l,h:longint;
  begin
     p:='abcdefghijklmnopqrstuvwxyz';
    readln(s);
      k:=1;
     repeat
      inc(z);
    t:=pos(s[z],p);
     h:=abs(t-k);
    if h>26-h then begin
                      //writeln(t,' ',k);
                      l:=l+(26-h);
                      end
                      else
                      begin
                      l:=l+h;
                      //writeln(t,' ',k);
                     end;
    k:=t;
     until(z=length(s));
   writeln(l);
 end.

uses math;
var s:string; i,position,ps,pf,ans:longint;
begin
    read(s);
    position:=ord('a');
    for i:=1 to length(s) do
    begin
        ps:=min(position, ord(s[i]));
        pf:=max(position, ord(s[i]));
        inc(ans, min(pf-ps, ps+26-pf));
        position:=ord(s[i]);
    end;
    write(ans);
end.

uses math;
var
 s:string;
 m,k,sum,i:longint;
begin
 readln(s);
 m:=97;
 for i:=1 to length(s) do
  begin
   k:=ord(s[i]);
   sum:=sum+min(abs(m-k),26-abs(m-k));
   m:=k;
  end;
 writeln(sum);
end.

program ideone;
var st:string;
	x1,x2,s,x,ma,i:longint;
begin
	readln(st);
	ma:=0;
	s:=0;
	for i:=1 to length(st) do
	begin
		x:=ord(st[i])-97;
		x1:=abs(x-ma);
		if x<ma then x2:=(26-ma+x)
		else x2:=(26-x+ma);
		if x1<x2 then s:=s+x1
		else s:=s+x2;
		ma:=x;
	end;
	writeln(s);
end.

uses math;
var
  s : ansistring;
  i,j,n,m,k : longint;
  c : char;

begin
  c:='a';
  readln(s);
  for i:=1 to length(s) do begin
    m:=m+min(min(abs(ord(s[i])-ord(c)),abs(ord(c)-ord('a'))+abs(ord('z')-ord(s[i])+1)),abs(ord(s[i])-ord('a'))+abs(ord('z')-ord(c)+1));
    c:=s[i];
  end;
  writeln(m);
end.

program a;
var i,j,l,aa,b,tmp,rez,rez1:longint;
    st,s:string;

begin
readln(st);
l:=97;
for i:=1 to length(st) do
begin
  b:=ord(st[i]);
  aa:=b;
  if b>l then
  begin
    tmp:=l;
    l:=b;
    b:=tmp;
  end;
  rez:=l-b;
  if rez>13 then
  begin
    rez:=26-rez;
  end;
  rez1:=rez1+rez;
  l:=aa;
end;
writeln(rez1);
readln;
readln;
end.

var
	s: ansistring;
	i,ans,a : longint;
	c : char;
	ar : array['a'..'z'] of longint;

begin
	readln(s);
	s := 'a'+s;
	i := 0;
	for c := 'a' to 'z' do begin
		inc(i);
		ar[c] := i;
	end;

	ans := 0;
	for i := 1 to length(s)-1 do begin
		a := ar[s[i]];
		if abs(ar[s[i+1]]-a) < (26-abs(ar[s[i+1]]-a)) then ans := ans + abs(ar[s[i+1]]-a)
		else ans := ans + (26-abs(ar[s[i+1]]-a));
	end;
	writeln(ans);
end.

var s:string;
    ans,i,now,x:longint;
begin
  now:=97;
  readln(s);
  ans:=0;
  for i:=1 to length(s) do
  begin
    x:=abs(ord(s[i])-now);
    if (26-x<x) then x:=26-x;
	ans:=ans+x;
    now:=ord(s[i]);
  end;
  writeln(ans);
end.

 var
     s: string;
     summ: integer;

 begin
   Readln(s);
   var letter_ptr := 97;
   var forward_val := 0;
   var backward_val := 0;
   for var i := 1 to s.Length do
   begin
     forward_val := ord(s[i]) - letter_ptr;
     backward_val := letter_ptr - ord(s[i]);
     if forward_val < 0 then forward_val += 26;
     if backward_val < 0 then backward_val += 26;
     summ += min(forward_val, backward_val);
     letter_ptr := ord(s[i]);
   end;
   Writeln(summ);
 end.

program Project1112;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function min(x, y: integer): integer;
begin
  if x < y then
    min := x
  else
    min := y;
end;

var
  s: string;
  x, i, y, sum: integer;

begin

  readln(s);

  sum := 0;

  x := 1;

  for i := 1 to length(s) do
  begin
    y := ord(s[i]) - 97 + 1;
    sum := sum + min(abs(x - y), 26 - abs(x - y));
    x := y;
  end;

  writeln(Sum);
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
    s: string;
    sum, i, x: integer;

function f(a, b: integer): integer;
begin
    if(a < b) then
        f := a
    else
        f := b;
end;

begin
    readln(s);
    sum := 0;
    i := 0;
    while(i < length(s)) do begin
        if(i = 0) then
            x := 1
        else
            x := ord(s[i]) - 96;
        sum := sum + f(abs(x - (ord(s[i + 1]) - 96)), abs(26 - abs(x - (ord(s[i + 1]) - 96))));
        inc(i);
    end;
    writeln(sum);
end.

      const eps=0.00000001;  besk1=200000008;  nn=10600000;
             { modul=1000000007;  }
   besk=8000000000000000000;
   label 22,1,2,3,4;
    type    real=extended;   double=extended;  int64=longint;
    mas=array [-100000..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[-10..nn] of longint;mass1=^mass;
    var limit,posi,posj,diam,modul,oldt,ot1,time,pos,prod,ll,rr,posx,posy,gr,n,dloch,newdl,oldn,oldm,ost,ot,aa,bb,cc,p1,p2,p3,p4,kk,znamzz,kolperiod,znam,tperiod,start,finish,kol1,kol2,lavel,x0,p0,t2,t1,jj,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,t,nm,m,smin,cen:int64;
   pp0,mask,k,i1,j1,step1, step, i,j:longint;
   y:mas;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    oldpos,finot,vv,znak:int64;
    aold:mas;
    xplus,xminus,px,py,xc,yc,xxc,yyc:extended;
    del:array[-1..1000001,-1..10] of longint;
    kolright,a,fact,next,prev,x,xx,yy,str,kol,s,name,d,dnew, voltay,l,r,c,z,day,b:mas;
    zzsum,zz:array[-10..12500000] of int64;
    ppole:array[-10..1002,-10..1005] of char;
    pole,ddd,dd1,dd2,dd3,dd4,dd,ddsum:array[-10..1201,-10..1220] of int64;
   newzan,marked,zanx,zany:array[-1000000..2000000] of boolean;
    sss1,sss2:string;
    udal,dl,och,newoch,place,stoim,nabor:array[-10..1200005] of int64;
     zanleft,zanright,zan,fl:array[-100000..10000000] of boolean;
    ss1,ss2,ss:string;ch,ch1,ch2:char;
    binkoef,numleft,numright,dr: array [-10..60,-10..60] of int64;
    s3,s2,s1:array[-100..200000] of string;

    grr :array [-10..2010,-10..2010] of boolean;
    maxpr:array[0..nn] of string;
    sss:array[-10..nn] of string;
     tx,ty,vel,x1,y1,ott,v0,v1,s0,t0:int64;
     zv:point;
     ugol,rot,temp,sumtemp,vol,newx,newy:array[-1000..400000] of int64;
     sq,sqx,sqy,ottt,aaa,bbb,ccc:extended;
     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

  function max(a,b:int64):int64;
begin max:=a; if b>a then max:=b;   end;

  function min(a,b:int64):int64;
begin min:=a; if b<a then min:=b;   end;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

{     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;
 }

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k shr 1); degg:=p*p mod modul;   end;

end;

{function prost(tt:longint):boolean;
var aa:extended; i,p,t:longint;  fl:boolean;
 begin
   fl:=true;
   aa:=tt;
   aa:=sqrt(aa);
   t:=round(aa);
   for i:=2 to t do if tt mod i=0 then fl:=false;

   prost:=fl;
 end;
 }
function arcsin(pp:extended):extended;
var tt:extended;
begin
   if (abs(pp)<=1+eps)and (abs(pp)>=1-eps)
   then if pp>0 then arcsin:=pi/2 else arcsin:=-pi/2;
   tt:=1-pp*pp;
   if abs(tt)>=0 then
     begin
        arcsin:=arctan(pp/sqrt(tt));

     end;

end;

function gl(ch:char):boolean;
begin
 if (ch='o')or (ch='a')or(ch='e')or(ch='u')or(ch='i') then gl:=true else gl:=false;

end;

begin

   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }
      readln(ss);
      n:=length(ss);
      for i:=1 to n do
        begin
           t:=ord(ss[i])-ord('a');
           ot:=ot+min(abs(t-pos),26-abs(t-pos));
           pos:=t;

        end;

  writeln(ot);

   1:          close(output);
end.

{$MODE OBJFPC}
{$R-,Q-,S-,I-}
{$OPTIMIZATION LEVEL2}
{$INLINE ON}

uses math;
const
        maxc=1000000000+69;
        modulo=1000000000+7;

type
        re=record
                x,y:longint;
        end;

procedure mofile;
begin
        {$IFDEF ONLINE_JUDGE}
        assign(input,'');
        reset(input);
        assign(output,'');
        rewrite(output);
        {$ELSE}
        assign(input,'');
        reset(input);
        assign(output,'');
        rewrite(output);
        {$ENDIF}
end;

procedure dongfile;
begin
        {$IFDEF ONLINE_JUDGE}
        close(input);
        close(output);
        {$ELSE}
        close(input);
        close(output);
        {$ENDIF}
end;

var
        kq:longint;
        s:ansistring;
        a:array['a'..'z'] of longint;

procedure xuli;
var
        c:char;
        i:longint;
begin
        readln(s);
        for c:='a' to 'z' do a[c]:=ord(c)-ord('a');
        c:='a';
        for i:=1 to length(s) do
        begin
                kq:=kq+min(abs(a[s[i]]-a[c]),26-abs(a[s[i]]-a[c]));
                c:=s[i];
        end;
        writeln(kq);
end;

begin
        mofile;
        xuli;
        dongfile;
end.

program Its1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, i, j, ans: integer;
  s: string;

begin
  readln(s);
  s := 'a' + s;
  ans := 0;
  for i := 2 to length(s) do begin
    if (abs(ord(s[i]) - ord(s[i - 1])) < abs(abs(ord(s[i]) - ord(s[i - 1])) -26)) then
      ans := ans + abs(ord(s[i]) - ord(s[i - 1]))
    else
      ans := ans + abs(abs(ord(s[i]) - ord(s[i - 1])) -26);
    //writeln(abs(ord(s[i]) - ord(s[i - 1])), ' ',abs(abs(ord(s[i]) - ord(s[i - 1])) -26))
  end;
  writeln(ans);
  readln;
end.

program nomera;

var
  i, t,  n, d, r, x: integer;
  s, p: string;

begin
  {ВВОД СТРОКИ Р}
  read(p);
  r := 1;
  s := 'abcdefghijklmnopqrstuvwxyz';
  {ДЛИНА СТРОКИ Р}
  for t := 1 to length(p) do
  begin
    n := n + 1;
  end;
  {СЧЕТ КОЛИЧЕСТВА ХОДОВ}
  for t := 1 to n do
  begin
    for i := 1 to 26 do
    begin
      if s[i] = p[t] then
        d := i - r;
      if (s[i] = p[t]) and (d < 0) then
        d := d * (-1);
      if (d > 13) then
        d := d - 26;
      if (s[i] = p[t]) and (d < 0) then
        d := d * (-1);
      if s[i] = p[t] then
        x := x + d;
      if s[i] = p[t] then
        if s[i] = p[t] then
          r := i;
    end;
  end;
  {ВЫВОД}
  writeln(x);
end.

const
        fi='';
        fo='';
var     f:text;
        st:string;
        Ans:longint;
        Rem:array['a'..'z'] of longint;
{}
procedure docf;
var     i:longint;
begin
        assign(f,fi); reset(f);
        readln(f,st);
        close(f);
end;
{}
procedure init;
var     i,j:longint;
        ch:char;
begin
        i:=0;
        for ch:='a' to 'z' do
        begin
                inc(i);
                rem[ch]:=i;
        end;
end;
{}
function min(a,b:longint):longint;
begin
        if a<b then min:=a else min :=b;
end;
{}
procedure lam;
var     i,j:longint;
        ht:char;
begin
        init; ht:='a';  ans:=0;
        for i:=1 to length(st) do
        begin
                ans:= ans + min(abs(rem[ht] - rem[st[i]]) , 26 - abs(rem[ht] -  rem[st[i]]));
                ht:=st[i];
        end;
end;
{}
procedure xuat;
begin
        assign(f,fo); rewrite(f);
        writeln(F,ans);
        close(F);
end;
{}
begin
        docf;
        lam;
        xuat;
end.

var s:string;
r,i,n,x,y:integer;
begin
readln(s);
y:=0;
n:=ord('z')-ord('a')+1;
for i:=1 to length(s) do begin
      x:=ord(s[i])-ord('a');
      if x>y then
          if x-y<n-x+y then r:=r+x-y else r:=r+n-x+y
          else
          if y-x<n-y+x then r:=r+y-x else r:=r+n-y+x;
      y:=x;
      end;
writeln(r);
end.

program O_O;
uses math;
const
	maxn=1000;
type
	ll=longint;
	arr=array[0..maxn,0..maxn] of ll;
var
	i,j,n,ans:ll;
	s:ansistring;
	sum:arr;
	k:char;
begin
	readln(s);
	s:='a'+s;
	n:=26;
	for i:=1 to n do
	for j:=1 to n do
	begin
		sum[i,j]:=min(abs(i-j),abs(i+n-j));
		sum[i,j]:=min(sum[i,j],abs(n-i+j));
	end;
	for i:=1 to length(S)-1 do
	begin
		inc(ans,sum[ord(s[i])-96,ord(s[i+1])-96]);
	end;
	writeln(ans);
end.

uses math;
var
 ch:char;
 s:string;
 i,ans:longint;
begin
 readln(s);
 ch:='a';
 for i:=1 to length(s) do
  begin
   ans:=ans+min(abs(ord(ch)-ord(s[i])),26-abs(ord(ch)-ord(s[i])));
   ch:=s[i];
  end;
 writeln(ans);
end.

var
s : string;
x,y,i,ans : longint;

function min(xx,yy : longint) : longint;
begin
 if xx < yy then min:=xx else min:=yy;
end;

begin

 readln(s);
 x:=97;

 for i:=1 to length(s) do
  begin
   y:=ord(s[i]);
   if x < y then ans:=ans+min(abs(x-y),(x-97+1) + 122 -y)
    else ans:=ans+min(abs(x-y),(122-x+1) + y - 97);
   x:=y;
   end;

 writeln(ans);

end.

program project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  d1,d2,i,j,ans,k:integer;
  s:string;
begin
  readln(s);
  k := 1;
  ans := 0;
  for i := 1 to length(s) do begin
    if(abs((ord(s[i])-96)-k)>=14)then begin
      inc(ans,26-abs(k-(ord(s[i])-96)));
      k := ord(s[i])-96;

    end
    else begin
      inc(ans,abs((ord(s[i])-96)-k));
      k := ord(s[i])-96;
    end;
  end;
  writeln(ans);
  readln;
  readln;
end.

var s:string;
d,i,n,b,a:integer;
begin
read (s);
d:=length(s);
b:=ord('a');
for i:=1 to d do
begin
a:=ord(s[i]);
if abs(b-a)>13 then n:=n+(26-(abs(b-a))) else n:=n+(abs(b-a));
b:=a;
end;
write(n);
end.

var
 h: char;
 s: shortstring;
 ans,i: longint;
begin
 readln(s);
 h:='a';
 for i:=1 to length(s) do
 begin
  if h < s[i] then
  begin
   if abs(ord('a')-ord(h))+abs(ord('z')-ord(s[i]))+1 < abs(ord(s[i])-ord(h)) then inc(ans,abs(ord('a')-ord(h))+abs(ord('z')-ord(s[i]))+1) else
   inc(ans,abs(ord(s[i])-ord(h)));
  end else
  begin
   if abs(ord('z')-ord(h))+abs(ord('a')-ord(s[i]))+1 < abs(ord(s[i])-ord(h)) then inc(ans,abs(ord('z')-ord(h))+abs(ord('a')-ord(s[i]))+1) else
   inc(ans,abs(ord(s[i])-ord(h)));
  end;

  h:=s[i];
 end;
 writeln(ans);
end.

var
 s:ansistring;
 x,y,sum,i:longint;
begin
 readln(s);
 x:=97;
 for i:=1 to length(s) do
  begin
   y:=abs(x-ord(s[i]));
   if 26-abs(x-ord(s[i]))<y then y:=26-abs(x-ord(s[i]));
   sum:=sum+y;
   x:=ord(s[i]);
  end;
 writeln(sum);
end.

var st,alf:string;
n,i,k,a,p,s1,s2:longint;
begin
  alf:='abcdefghijklmnopqrstuvwxyz';
  readln(st);
  n:=length(st);
  a:=1;
  for i:=1 to n do
    begin
      p:=pos(st[i],alf);
      s1:=abs(a-p);
      s2:=26-s1;
      if s1<s2 then k:=k+s1
        else k:=k+s2;
      a:=p;
    end;
  writeln(k);
end.

var s : String;
  k1, k2, rast1, rast2, kolv, i, len : Integer;
begin
  readln(s);
  len := Length(s);
  k1 :=  Ord('a');
  For i := 1 to len do
    begin
      k2 := Ord(s[i]);
      If k1 < k2 then begin
                        rast1 := k2 - k1;
                        rast2 := 26 - rast1;
                      end
                 else begin
                        rast1 := k1 - k2;
                        rast2 := 26 - rast1;
                      end;
      kolv := kolv + Min(rast1,rast2);
      k1 := k2;
    end;
  writeln(kolv);
end.

var
      i,sum:longint;
      awal:char;
      s:ansistring;
      huruf:array['a'..'z'] of integer;
begin
      i:=1;
      for awal:='a' to 'z' do begin
            huruf[awal]:=i;
            inc(i);
      end;
      readln(s);
      awal:='a'; sum:=0;
      for i:=1 to length(s) do begin
            if ((26-(abs(huruf[awal]-huruf[s[i]])))<=(abs(huruf[awal]-huruf[s[i]]))) then begin
                  sum:=sum+(26-(abs(huruf[awal]-huruf[s[i]])));
            end
            else if (((26-(abs(huruf[awal]-huruf[s[i]])))>(abs(huruf[awal]-huruf[s[i]])))) then begin
                  sum:=sum+(abs(huruf[awal]-huruf[s[i]]));
            end;
            awal:=s[i];
      end;
      writeln(sum);
end.

var j,i,pov,m1,m2,b,c,n:longint;
    s,s1:string;
    a:array[0..101]of byte;

begin
 a[0]:=1;
 readln(s);
 s1:='abcdefghijklmnopqrstuvwxyz';
 for i:=1 to length(s) do
  for j:=1 to length(s1) do
   if s[i]=s1[j] then a[i]:=j;

   pov:=0;
 n:=length(s)-1;
 for j:=0 to n do
  begin
   m1:=0;
   m2:=0;
   c:=0;
   b:=0;
   if a[j]>a[j+1] then
     begin
      c:=a[j];
      b:=a[j+1];
     end
    else
     begin
      c:=a[j+1];
      b:=a[j];
     end;
    m1:=abs(a[j]-a[j+1]);
    m2:=26-c+b;

    if m1<m2 then pov:=pov+m1
             else pov:=pov+m2;

  end;

 writeln(pov);
end.

var i,j,m,n,ans,a,now:longint;
    s:ansistring;
begin
    readln(s);
    now:=0;
    for i:=1 to length(s) do begin
        a:=ord(s[i])-ord('a');
        if abs(a-now)<=13 then ans:=ans+abs(a-now) else ans:=ans+26-abs(a-now);
        now:=a;
    end;
    writeln(ans);
end.

uses math;
var s:string; i,position,ps,pf,ans:longint;
begin
    read(s);
    position:=ord('a');
    for i:=1 to length(s) do
    begin
        ps:=min(position, ord(s[i]));
        pf:=max(position, ord(s[i]));
        inc(ans, min(pf-ps, ps+26-pf));
        position:=ord(s[i]);
    end;
    write(ans);
end.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function	Min(a, b: Integer): Integer;
begin
	if a<b then Result := a
	else Result := b;
end;
function	Dist(a, b: Integer): Integer;
begin
	if a>=b then Result := a-b
	else Result := a+26-b;
end;

var
	s: string;
	i, l, cnt, c1, c2: Integer;

begin
	Readln(s);
	l := Length(s);
	cnt := 0;
	c1 := Ord('a');
	for i:=1 to l do
	begin
		c2 := Ord(s[i]);
		Inc(cnt, Min(Dist(c1, c2), Dist(c2, c1)));
		c1 := c2;
	end;
	Writeln(cnt);
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  s: string;
  c: char;
  sum, i: integer;

function ch(a, b: char): integer;
begin
  if(ord(a) > ord(b))then begin
    if(abs((ord(a) - 96) - (ord(b) - 96)) < abs(26 + (ord(b) - 96) - (ord(a) - 96)))then
      ch := abs((ord(a) - 96) - (ord(b) - 96))
    else
      ch := abs(26 + (ord(b) - 96) - (ord(a) - 96));
  end
  else begin
   if(abs((ord(a) - 96) - (ord(b) - 96)) < abs(26 - (ord(b) - 96) + (ord(a) - 96)))then
      ch := abs((ord(a) - 96) - (ord(b) - 96))
    else
      ch := abs(26 - (ord(b) - 96) + (ord(a) - 96));
  end;
end;

begin
  readln(s);
  sum := 0;
  c := 'a';
  for i := 1 to length(s) do begin
    sum := sum + ch(s[i], c);
    c := s[i];
  end;
  writeln(sum);
  readln;
  readln;
end.

Var
    S: String;
    Ans: LongInt;

Begin
    Readln(S);
    Ans += min(abs(ord('a') - ord(S[1])), 26 - abs(ord('a') - ord(S[1])));
    For Var i := 1 to Length(S) - 1 do Begin
      Ans += min(abs(ord(S[i]) - ord(S[i + 1])), 26 - abs(ord(S[i]) - ord(S[i + 1])));
    End;
    Write(Ans)
End.

var
sum,i:integer;
s:string;

Begin
readln (s);
sum:=0;
s:='a'+s;
For i:=1 to length(s)-1 do
begin
 if abs(ord(s[i+1])-ord(s[i]))>=14 then
 sum:=sum+26-abs(ord(s[i+1])-ord(s[i]))
  else sum:=sum+abs(ord(s[i+1])-ord(s[i]));
end;
Write(sum);
End.

USES Math;
CONST
    tfi = '';
    tfo = '';
VAR
    fi,fo               : text;

Procedure process;
    Var
        i,res: longint;
        s: string;
        c: char;
    Begin
        Readln(fi,s);
        c:='a'; res:=0;
        For i:=1 to length(s) do
            begin
                res:=res+min(abs(ord(s[i])-ord(c)),26-abs(ord(s[i])-ord(c)));
                c:=s[i];
            end;
        write(fo,res);
    End;

BEGIN
    assign(fi,tfi); reset(fi);
    assign(fo,tfo); rewrite(fo);
        process;
    close(fi); close(fo);
END.

//type ansistring = string;

procedure swap (var a,b:longint);
var c:longint;
begin
    c:=a;
    a:=b;
    b:=c;
end;

function get_min_moves(var s:longint; f:longint):longint;
var start:longint;
begin
    start:=f;
    if s > f then
        swap(s,f);
    if (f - s) < (26 + s - f) then
        get_min_moves:= f-s
    else
        get_min_moves:= 26+s-f;
    s:=start;
end;

function count_moves(s:ansistring):longint;
var k,i,start:longint;
begin
    k:=0;
    start:=1;
    for i:= 1 to length(s) do
        inc(k,get_min_moves(start,ord(s[i])-96));

    count_moves:=k;
end;

var s:ansistring;
begin
    readln(s);
    writeln(count_moves(s));
end.

var
  i,j,k,len:longint;
  s:string;
  ans:longint;
  p:char;
begin
  readln(s);
  len:=length(s);
  p:='a';
  for i:=1 to len do
  begin
    if (s[i]>p) then
    begin
      if ord(s[i])-ord(p)<26-(ord(s[i])-ord('a')+1)+ord(p)-ord('a')+1 then
        inc(ans,ord(s[i])-ord(p))
      else
        inc(ans,26-(ord(s[i])-ord('a')+1)+ord(p)-ord('a')+1);
      p:=s[i];
    end
    else
    begin
      if ord(p)-ord(s[i])<26-(ord(p)-ord('a')+1)+ord(s[i])-ord('a')+1 then
        inc(ans,ord(p)-ord(s[i]))
      else
        inc(ans,26-(ord(p)-ord('a')+1)+ord(s[i])-ord('a')+1);
      p:=s[i];
    end;
  end;
  writeln(ans);
end.

var
a,s:string;
p,o:longint;
begin
readln(a);
s:='abcdefghijklmnopqrstuvwxyz';
if pos(a[1],s)-1<27-pos(a[1],s)
then o:=pos(a[1],s)-1
else o:=27-pos(a[1],s);
for p:=2 to length(a) do
if abs(pos(a[p],s)-pos(a[p-1],s))<26-abs(pos(a[p],s)-pos(a[p-1],s))
then inc(o,abs(pos(a[p],s)-pos(a[p-1],s))) else
inc(o,26-abs(pos(a[p],s)-pos(a[p-1],s)));
write(o); end.

uses math;
var
 s:string;
 m,k,sum,i:longint;
begin
 readln(s);
 m:=97;
 for i:=1 to length(s) do
  begin
   k:=ord(s[i]);
   sum:=sum+min(abs(m-k),26-abs(m-k));
   m:=k;
  end;
 writeln(sum);
end.

var s:string;
    i,n,k,point,max,min:integer;
    mas:array[1..26] of char;
function Index(g:string):byte;
var i:integer;
begin
for i:= 1 to 26 do
 if (mas[i] = g) then Index:=i;
end;
begin
readln(s);
for i:= 97 to 122 do
  mas[i-96]:=chr(i);
k:=1;
for i:= 1 to length(s) do
 begin
  point:=Index(s[i]);
  if (point > k) then
   begin
    max:=point;
    min:=k;
   end
  else
   begin
    max:=k;
    min:=point;
   end;
  if (abs(max-min)<abs(26-max+min)) then
   begin
    n:=n+abs(max-min);
    k:=point;
   end
  else
   begin
    n:=n+abs(26-max+min);
    k:=point;
   end;
 end;
writeln(n);
end.

.............k...........point.......26

var s:string;
    n,j,i,min,max:longint;
    t:array ['a'..'z'] of longint
    = (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);
begin
  readln(s);
  n:=0;
  j:=t['a'];
  for i:=1 to length(s) do
    begin
      if j < t[s[i]] then begin min:=j; max:=t[s[i]]; end
        else begin min:=t[s[i]]; max:=j; end;

      if max-min < 26-max+min then n:=n+max-min
        else n:=n+26-max+min;
      j:=t[s[i]];    // writeln(j,' ',n);
    end;

  writeln(n);
end.

var
  s,s1,s2:string;
  i,j,n,m,k,x1,x2,z:longint;
begin
  read(s);
  z:=97;
  for i:=1 to length(s) do
     begin
       x1:=abs(z-ord(s[i]));
       if 26-x1<x1 then x1:=26-x1;
       k:=k+x1;
       z:=ord(s[i]);
end;
write(k);
end.

var s:string;
    ans,i,now,x:longint;
begin
  now:=97;
  readln(s);
  ans:=0;
  for i:=1 to length(s) do
  begin
    x:=abs(ord(s[i])-now);
    if (26-x<x) then x:=26-x;
	ans:=ans+x;
    now:=ord(s[i]);
  end;
  writeln(ans);
end.

program aaa;
var s : string;
    i, res, abc : integer;
begin
  read(s);
  if(abs(ord(s[1])-97)<13) then
  res:=abs(ord(s[1])-97)
  else res:=abs(ord(s[1])-97-26);
  abc:=ord(s[1]);
  for i:=2 to length(s) do
  begin
  if(abs(ord(s[i])-abc)<13) then
    abc:=abs(ord(s[i])-abc)
    else
    abc:=abs(abs(abc-ord(s[i]))-26);
    res:=res+abc;
    abc:=ord(s[i]);
  end;
  writeln(res);
end.

var s:string;ch:char; i:longint;sum:int64;
function min(a,b:longint):longint;
begin
  if a<b then exit(a) else exit(b);
end;
begin
  readln(s);
  ch:='a';
  for i:=1 to length(s) do
  begin
    if s[i]>ch then sum:=sum+min(ord(s[i])-ord(ch),ord(ch)+26-ord(s[i]));
    if ch>s[i] then sum:=sum+min(ord(ch)-ord(s[i]),ord(s[i])+26-ord(ch));
    ch:=s[i];
  end;
  writeln(sum);
end.

var st,alf:string;
sim:char;
n,i,k1,k2,sum,p,p2:longint;
begin
  readln(st);
  n:=length(st);
  alf:='abcdefghijklmnopqrstuvwxyz';
  p:=pos(st[1],alf);
  if p>1 then begin
                k1:=p-1;
                k2:=(26-p)+1;
                if k1<=k2 then sum:=k1
                          else sum:=k2;
              end;
  for i:=2 to n do
    begin
      sim:=st[i];
      p2:=pos(sim,alf);
      k1:=abs(p-p2);
      if p>=p2 then k2:=(26-p)+p2
               else k2:=(26-p2)+p;
      if k1<=k2 then sum:=sum+k1
                else sum:=sum+k2;
      p:=p2;
    end;
  writeln(sum);
end.

uses math;
var s,ss:ansistring;
i,k,l,m,n,q,w,e,r,t:longint;
begin
readln(s); q:=1;
ss:='abcdefghijklmnopqrstuvwxyz';
for i:=1 to length(s) do
begin
k:=pos(s[i],ss);
t:=t+min(abs(q-k),26-abs(q-k));
q:=k;
end;
writeln(t);
end.

program ideone;
var st:string;
	x1,x2,s,x,ma,i:longint;
begin
	readln(st);
	ma:=0;
	s:=0;
	for i:=1 to length(st) do
	begin
		x:=ord(st[i])-97;
		x1:=abs(x-ma);
		if x<ma then x2:=(26-ma+x)
		else x2:=(26-x+ma);
		if x1<x2 then s:=s+x1
		else s:=s+x2;
		ma:=x;
	end;
	writeln(s);
end.

uses math;
var  i,p,ans:longint;
     s,s1:ansistring;
 begin
  readln(s);
  s1:='a'+s;
 // writeln(s1);

  for i:=2 to length(s1) do
   begin
    p:=abs(ord(s1[i])-ord(s1[i-1]));
    p:=min(p,26-p);
    inc(ans,p);
   end;
  writeln(ans);

 end.

var
  st:string;
  n,i,t,ans:longint;
function min(a,b:longint):longint;
  begin if (a<b) then exit(a) else exit(b); end;
begin
  readln(st);
  st:='a'+st;
  n:=length(st);
  for i:=2 to n do
    begin
	  t:=abs(ord(st[i-1])-ord(st[i]));
      ans:=ans+min(t,26-t);
	end;
  writeln(ans);
end.

uses math;

const fi = '';

var     s: ansistring;
        res, i, x: longint;
        f: text;

begin
        assign(f, fi); reset(f);
        readln(f, s);
        close(f);

        s:= 'a'+s; res:= 0;
        for i:= 2 to length(s) do
        begin
                x:= abs(ord(s[i])-ord(s[i-1]));
                res:= res+min(x, 26-x);
        end;
        writeln(res);
        readln;
end.

var
	s:string;
	cur,j:char;
	k,i,ans:integer;
	a:array['a'..'z'] of integer;
begin
	k:=0;
	ans:=0;
	for j:='a' to 'z' do
		Begin
			inc(k);
			a[j]:=k;
		End;
	readln(s);
	cur:='a';
	for i:=1 to length(s) do
		Begin
			if cur<=s[i] then
			Begin
				if(abs(a[s[i]]-a[cur])<abs(26-a[s[i]]+a[cur])) then
					ans:=ans+abs(a[s[i]]-a[cur])
				else
					ans:=ans+abs(26-a[s[i]]+a[cur]);
			End
			else
			Begin
				if(abs(a[s[i]]-a[cur])<abs(26-a[cur]+a[s[i]])) then
						ans:=ans+abs(a[s[i]]-a[cur])
					else
						ans:=ans+abs(26-a[cur]+a[s[i]]);
			End;
			cur:=s[i];
		End;
	writeln(ans);
end.

var
 s : string;
 i,n,a,b,c,d,ans : integer;

begin
 readln(s);
 insert('a',s,1);
 n := length(s);
 for i:=2 to n do
  begin
   a := abs(ord(s[i]) - ord(s[i-1]));
   b := abs(26 + ord(s[i-1]) - ord(s[i]));
   c := abs(26 + ord(s[i]) - ord(s[i-1]));
   if(a>b) then d := b else d := a;
   if(d>c) then d := c;
   ans := ans + d;
  end;
 writeln(ans);
end.

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O-,P+,Q-,R+,S+,T-,U-,V+,W-,X+,Y+,Z1}
{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $00100000}
{$IMAGEBASE $00400000}
{$APPTYPE CONSOLE}

const filename='qc';
      oo=maxlongint div 2;
      eps=1e-6;
      eee=1e-21;

var qi,ci:array[0..1000000]of int64;
    qq,cc,a,aa:array[0..1000000]of longint;
    qc,he:array[0..2014,0..2014]of longint;
    ss,sss:array[0..10000]of string;
    s,s0,s1,s2,s3:string;
    x,y,z,xx,yy,zz,x0,y0,z0,x1,x2,x3,y1,y2,y3,z1,z2,z3:extended;
    res64,n64,nn64:int64;
    w,nt,ii,jj,n,m,q,c,q0,c0,q1,c1,q2,c2,q3,c3:longint;
    nn,mm,kk,u,v,res,cur,max,min:longint;
    test,ll,t,tt:longint;
    b,bb,bbb,b0,b1,b2,b3:boolean;
    ccc:char;

function mx(q,c:longint):longint;
  begin
  if q>c then mx:=q else mx:=c;
  end;

function mn(q,c:longint):longint;
  begin
  if q<c then mn:=q else mn:=c;
  end;

procedure init;
  begin
  assign(input,filename+'.in');
  assign(output,filename+'.out');
  reset(input);
  rewrite(output);
  end;

procedure fin;
  begin
  close(input);
  close(output);
  end;

procedure clear;
var i,j,k:longint;
  begin
  fillchar(qc,sizeof(qc),0);
  end;

procedure load;
var i,j,k:longint;
  begin
  end;

procedure save;
var i,j,k:longint;
  begin
  end;

procedure swap(var q,c:longint);
var i:longint;
    begin
    i:=q; q:=c; c:=i;
    end;

procedure qcsort(q,c:longint);
var h,e,d,x:longint;
    ass:boolean;
    begin
    if q>=c then exit;
    x:=q+random(c-q+1);
    swap(qc[q,0],qc[x,0]);
    swap(qc[q,1],qc[x,1]);
    h:=q; e:=c; d:=1;
    while h+d<=e do
      begin
      if qc[h,0]=qc[h+d,0] then inc(d) else
      if qc[h,0]<qc[h+d,0] then
        begin swap(qc[h+d,0],qc[e,0]); swap(qc[h+d,1],qc[e,1]); dec(e); end
      else begin swap(qc[h,0],qc[h+d,0]); swap(qc[h,1],qc[h+d,1]); inc(h); end
      end;
    qcsort(q,h-1);
    qcsort(h+d,c);
    end;

procedure qqcsort(q,c:longint);
var h,e,d,x:longint;
    ass:boolean;
    begin
    if q>=c then exit;
    x:=q+random(c-q+1);
    swap(qq[q],qq[x]);
    h:=q; e:=c; d:=1;
    while h+d<=e do
      begin
      if qq[h]=qq[h+d] then inc(d) else
      if qq[h]<qq[h+d] then
        begin swap(qq[h+d],qq[e]); dec(e); end
      else begin swap(qq[h],qq[h+d]); inc(h); end
      end;
    qqcsort(q,h-1);
    qqcsort(h+d,c);
    end;

procedure run;
var i,j,k:longint;
  begin
  //init;
  readln(s);
  n:=length(s);
  res:=0; cur:=0;
  s:='a'+s;
  for i:=1 to n do
    begin
    cur:=ord(s[i+1])-ord(s[i]);
    if cur<0 then inc(cur,26);
    if cur>13 then cur:=26-cur;
    inc(res,cur);
    end;
  writeln(res);
  //fin;
  end;

begin
nt:=1;
//readln(nt);
for test:=1 to nt do
  begin
  clear;
  load;
  run;
  save;
  end;
end.

var st,alf:string;
n,i,k,a,p,s1,s2:longint;
begin
  alf:='abcdefghijklmnopqrstuvwxyz';
  readln(st);
  n:=length(st);
  a:=1;
  for i:=1 to n do
    begin
      p:=pos(st[i],alf);
      s1:=abs(a-p);
      s2:=26-s1;
      if s1<s2 then k:=k+s1
        else k:=k+s2;
      a:=p;
    end;
  writeln(k);
end.

var s    : string;
    i, t : byte;
    moves: word;

function min(a, b: byte): byte;
   begin
      if a < b then exit(a);
      exit(b);
   end;

BEGIN
   readln(s);

   t    := ord(s[1]) - ord('a');
   moves:= min(t, 26 - t);

   for i:= 2 to length(s) do
      begin
         t:= abs(ord(s[i]) - ord(s[i-1]));
         inc(moves, min(t, 26 - t));
      end;

   writeln(moves);

   readln
END.

{$mode objfpc}

uses math;

var
  s:ansistring;
  i,kq,x,y:integer;
  o:char;

begin
  readln(s);
  o:='a';
  for i:=1 to length(s) do
    begin
      x:=ord(o) - ord(s[i]);
      if x < 0 then x:=x + 26;
      y:=ord(s[i]) - ord(o);
      if y < 0 then y:=y + 26;
      kq:=kq + min(x,y);
      o:=s[i];
    end;
  write(kq);
end.

var s:string;i,k,p:integer;
begin
  readln(s);
  p:=1;
  k:=0;

  for i:=1 to length(s)do begin
    if abs(pos(s[i],'abcdefghijklmnopqrstuvwxyz')-p)>26-abs(pos(s[i],'abcdefghijklmnopqrstuvwxyz')-p)
    then k:=k+26-abs(pos(s[i],'abcdefghijklmnopqrstuvwxyz')-p) else
    k:=k+abs(pos(s[i],'abcdefghijklmnopqrstuvwxyz')-p);
    p:=pos(s[i],'abcdefghijklmnopqrstuvwxyz');
    end;

writeln(k);
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var n : longint;
    i, j, x, y : longint;
    s : string;
begin

  readln(s);

  n := 0;

  s := 'a' + s;

  for i := 2 to length(s) do
  begin
    x := ord(s[i-1]) - ord(s[i]);
    y := ord(s[i]) - ord(s[i-1]);
    if x < 0 then
      x := x + 26
    else
      y := y + 26;
    n := n + min(x, y);
  end;

  writeln(n);
end.

uses math;
var
 s:string;
 m,k,sum,i:longint;
begin
 readln(s);
 m:=97;
 for i:=1 to length(s) do
  begin
   k:=ord(s[i]);
   sum:=sum+min(abs(m-k),26-abs(m-k));
   m:=k;
  end;
 writeln(sum);
end.

Var
s:string;
n,p,i,t:byte;
c,a,b:integer;

Begin
readln(s);
n:=length(s);
p:=0;
c:=0;

For i:=1 to n do
  begin
    t:=ord(s[i])-ord('a');
    if t<p then begin a:=p-t; b:=26-p+t end
    else begin a:=t-p; b:=26-t+p end;
    if a>b then inc(c,b) else inc(c,a);
    p:=t
  end;

Write(c);
End.

{$mode objfpc}

uses math;

const
 inputfile='';
 outputfile='';

var
 fi,fo:text;
 s:ansistring;
 res,i:longint;

procedure openf;
begin
 assign(fi,inputfile);
 reset(fi);
 assign(fo,outputfile);
 rewrite(fo);
end;

procedure input;
begin
 read(fi,s);
end;

procedure process;
begin
 res:=min(abs(ord(s[1])-97),26-abs(ord(s[1])-97));
 for i:=2 to length(s) do
  begin
   res:=res+min(abs(ord(s[i])-ord(s[i-1])),26-abs(ord(s[i])-ord(s[i-1])));
  end;
end;

procedure output;
begin
 write(fo,res);
end;

procedure closef;
begin
 close(fi);
 close(fo);
end;

begin
 openf;
 input;
 process;
 output;
 closef;
end.

var st:string;
i,n,start,s:integer;
begin
read(st);
start:=ord('a');
for i:=1 to length(st) do begin
n:=ord(st[i]);
if n>=start then  begin
if n-start<=13 then s:=s+(n-start) else s:=s+(26-(n-start));
end else begin
if start-n<=13 then s:=s+(start-n) else s:=s+(26-(start-n));
end;
start:=ord(st[i]);
end;
writeln(s);
end.

uses math;
var s:string;
    i,n,ans:longint;
begin
  readln(s);
  ans:=min(abs(ord('a')-ord(s[1])),26-abs(ord('a')-ord(s[1])));
  for i:=2 to length(s) do
  inc(ans,min(abs(ord(s[i])-ord(s[i-1])),26-abs(ord(s[i])-ord(s[i-1]))));
  writeln(ans);
end.

program embosser;
var i,a:integer;
S:string;
begin
 readln(S);
 if ord(S[1])-109>0 then a:=123-ord(S[1])
 else a:=ord(S[1])-96;
 for i:=2 to length(S) do
 begin
  if ord(S[i])>ord(S[i-1]) then
  begin
   if (ord(S[i])-13)>ord(S[i-1]) then a:=a+(26+ord(S[i-1])-ord(S[i]))
   else
   a:=a+(ord(S[i])-ord(S[i-1]));
  end
  else
  begin
   if (ord(S[i-1])-13)>ord(S[i]) then a:=a+(26+ord(S[i])-ord(S[i-1]))
   else
   a:=a+(ord(S[i-1])-ord(S[i]));
  end;
 end;
 writeln(a);
end.

Program museum;
var
i,d,f,j,k,otv,x:integer;
s,e:string;
a:array[1..100] of string;
b:array[1..100] of string;
m:array[1..100] of string;
begin
readLn(s);
d:=length(s);
e:='nopqrstuvwxyzabcdefghijklm';
f:=length(e);
otv:=0;

for i:=1 to d do
begin
  for j:=1 to f do
    if s[i]=e[j] then
  begin
    k:=j-14;
    otv:=otv+abs(k);

  end;
  if k>0 then
  begin
    for j:=1 to f-k do
    m[j]:=e[j+k];
    for j:=f-k+1 to f do
    m[j]:=e[j-(f-k)];
    e:='';
    for j:=1 to f do
    e:=e+m[j];
  end
  else
  if k<0 then
  begin
    k:=abs(k);
    for j:=1 to k do
    m[j]:=e[f-k+j];
    for j:=k+1 to f do
    m[j]:=e[j-k];
    e:='';
    for j:=1 to f do
    e:=e+m[j];
  end
  else e:='nopqrstuvwxyzabcdefghijklm';

end;
   writeln(otv);
end.

var n,m,p,r,a,x,y,s,albe,negre,c : longint;
b,v,min,max : array[0..100000] of longint;
da : boolean;
z : ansistring;
begin

	readln(z);
	writeln(ord('z'));
	p:=1;
	for c:=1 to length(z) do
	begin

		r:=abs(96-ord(z[c]));
		writeln((abs(r-p)),' ',(26-abs(r-p)));
		if (abs(r-p))>=(26-abs(r-p)) then
		s:=s+(26-abs(r-p)) else
		if (abs(r-p))<(26-abs(r-p)) then
		s:=s+(abs(r-p));

		p:=r;
	end;

	writeln(s);
end.

var n,m,p,r,a,x,y,s,albe,negre,c : longint;
b,v,min,max : array[0..100000] of longint;
da : boolean;
z : ansistring;
begin

	readln(z);
	writeln(ord('z'));
	p:=1;
	for c:=1 to length(z) do
	begin

		r:=abs(96-ord(z[c]));
		if (abs(r-p))>=(26-abs(r-p)) then
		s:=s+(26-abs(r-p)) else
		if (abs(r-p))<(26-abs(r-p)) then
		s:=s+(abs(r-p));

		p:=r;
	end;

	writeln(s);
end.

var i,j:char;
    k,rotation:integer;
    s:string;
    alphabet:array['a'..'z'] of integer;
begin
        k:=1;
        for i:='a' to 'm' do
        begin
                        alphabet[i]:=k;
                        k:=k+1;
        end;
        k:=0;
        for j:='z' downto 'n' do
        begin
                        alphabet[j]:=k;
                        k:=k-1;
        end;

        readln(s);
        rotation:=abs(alphabet[s[1]]-1);
        for k:=2 to length(s) do
        begin
            rotation:=rotation+abs(alphabet[s[k]]-alphabet[s[k-1]]);
        end;
        writeln(rotation);
end.

uses math;
var i,j:char;
    k,rotation:integer;
    s:string;
    count:array['a'..'z'] of integer;
begin
        k:=1;
        for i:='a' to 'm' do
        begin
                        count[i]:=k;
                        k:=k+1;
        end;
        k:=0;
        for j:='z' downto 'n' do
        begin
                        count[j]:=k;
                        k:=k-1;
        end;
        readln(s);
        if count[s[1]]-count['a']<=13 then rotation:=abs(count[s[1]]-count['a'])
        else rotation:=abs(26-abs(count[s[1]]-count['a']));
        writeln(rotation);
        for k:=2 to length(s) do
        begin
            if abs(count[s[k]]-count[s[k-1]])<=13 then rotation:=rotation+abs(count[s[k]]-count[s[k-1]])
            else rotation:=rotation+abs(26-abs(count[s[k]]-count[s[k-1]]));
            writeln(rotation);
        end;
        writeln(rotation);
end.

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O-,P+,Q-,R+,S+,T-,U-,V+,W-,X+,Y+,Z1}
{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $00100000}
{$IMAGEBASE $00400000}
{$APPTYPE CONSOLE}

const filename='qc';
      oo=maxlongint div 2;
      eps=1e-6;
      eee=1e-21;

var qi,ci:array[0..1000000]of int64;
    qq,cc,a,aa:array[0..1000000]of longint;
    qc,he:array[0..2014,0..2014]of longint;
    ss,sss:array[0..10000]of string;
    s,s0,s1,s2,s3:string;
    x,y,z,xx,yy,zz,x0,y0,z0,x1,x2,x3,y1,y2,y3,z1,z2,z3:extended;
    res64,n64,nn64:int64;
    w,nt,ii,jj,n,m,q,c,q0,c0,q1,c1,q2,c2,q3,c3:longint;
    nn,mm,kk,u,v,res,cur,max,min:longint;
    test,ll,t,tt:longint;
    b,bb,bbb,b0,b1,b2,b3:boolean;
    ccc:char;

function mx(q,c:longint):longint;
  begin
  if q>c then mx:=q else mx:=c;
  end;

function mn(q,c:longint):longint;
  begin
  if q<c then mn:=q else mn:=c;
  end;

procedure init;
  begin
  assign(input,filename+'.in');
  assign(output,filename+'.out');
  reset(input);
  rewrite(output);
  end;

procedure fin;
  begin
  close(input);
  close(output);
  end;

procedure clear;
var i,j,k:longint;
  begin
  fillchar(qc,sizeof(qc),0);
  end;

procedure load;
var i,j,k:longint;
  begin
  end;

procedure save;
var i,j,k:longint;
  begin
  end;

procedure swap(var q,c:longint);
var i:longint;
    begin
    i:=q; q:=c; c:=i;
    end;

procedure qcsort(q,c:longint);
var h,e,d,x:longint;
    ass:boolean;
    begin
    if q>=c then exit;
    x:=q+random(c-q+1);
    swap(qc[q,0],qc[x,0]);
    swap(qc[q,1],qc[x,1]);
    h:=q; e:=c; d:=1;
    while h+d<=e do
      begin
      if qc[h,0]=qc[h+d,0] then inc(d) else
      if qc[h,0]<qc[h+d,0] then
        begin swap(qc[h+d,0],qc[e,0]); swap(qc[h+d,1],qc[e,1]); dec(e); end
      else begin swap(qc[h,0],qc[h+d,0]); swap(qc[h,1],qc[h+d,1]); inc(h); end
      end;
    qcsort(q,h-1);
    qcsort(h+d,c);
    end;

procedure qqcsort(q,c:longint);
var h,e,d,x:longint;
    ass:boolean;
    begin
    if q>=c then exit;
    x:=q+random(c-q+1);
    swap(qq[q],qq[x]);
    h:=q; e:=c; d:=1;
    while h+d<=e do
      begin
      if qq[h]=qq[h+d] then inc(d) else
      if qq[h]<qq[h+d] then
        begin swap(qq[h+d],qq[e]); dec(e); end
      else begin swap(qq[h],qq[h+d]); inc(h); end
      end;
    qqcsort(q,h-1);
    qqcsort(h+d,c);
    end;

procedure run;
var i,j,k:longint;
  begin
  //init;
  readln(s);
  n:=length(s);
  res:=0; cur:=0;
  for i:=1 to n do
    begin
    cur:=ord(s[i])-ord('a')-cur;
    if cur<0 then inc(cur,26);
    if cur>13 then cur:=26-cur;
    inc(res,cur);
    end;
  writeln(res);
  //fin;
  end;

begin
nt:=1;
//readln(nt);
for test:=1 to nt do
  begin
  clear;
  load;
  run;
  save;
  end;
end.


In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states: In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the Pythagorean equation:a2 + b2 = c2where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.  Given n, your task is to count how many right-angled triangles with side-lengths a, b and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

The only line contains one integer n (1 ≤ n ≤ 104) as we mentioned above.

Print a single integer — the answer to the problem.

var
 kol,n,i,j:longint;
 ans:extended;
begin
{ assign(input,'input.txt');
 reset(input);
 assign(output,'output.txt');
 rewrite(output); }
 readln(n);
 for i:=1 to n do
 begin
  for j:=i to n do
  begin
   ans:=sqrt(sqr(i)+sqr(j));
   if (ans<=n)and(frac(ans)=0) then inc(kol);
  end;
 end;
 writeln(kol);
end.

var b,c,i,j,n,x:longint;
begin
 x:=0;
 read(n);
  for i:=1 to n do
   begin
    c:=sqr(i);
     for j:=1 to i do
      begin
       b:=sqr(j);
        if (c<>b) and (sqrt(c-b)=trunc(sqrt(c-b))) and (sqrt(c-b)<=j) then
         inc(x);
      end;
   end;
  write(x);
end.

uses math,sysutils;
var r,kol,g,xv,l,d,res,min1,z,j,max1,l1:integer;x3,y3,x1,y1,x2,y2,x,y,i,k:integer;
    ch:char;s,s1,ss:string;a,e,n,e1,b,c:integer;u:boolean;
    v,v1:real;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n);
  e:=trunc(sqrt(n));
  kol:=0;
  for i:=1 to n do
     for z:=i to 100000 do
        if sqrt(i*i+z*z)>n
        then break
        else Begin
                v:=sqrt(i*i+z*z);
                if v<>round(v)
                then continue;
                if (i+z>v) and (i+v>z) and (v+z>i)
                then inc(kol);
             end;
  writeln(kol);
end.

var i,j,n,r:integer;
begin
readln(n);
for i:=1 to n do
    for j:=i to n do
        if ((frac(sqrt(i*i+j*j))=0) and ((sqrt(i*i+j*j))<=n)) then r:=r+1;
writeln(r);
readln;
end.

var n,i,j,ans:longint;

begin
//assign(input,'bdgls.in');reset(input);
//assign(output,'bdgls.out');rewrite(output);
read(n);
for i:=1 to n do
  for j:=i to n do
    if (sqrt(i*i+j*j)<=n) and (trunc(sqrt(i*i+j*j))=sqrt(i*i+j*j)) then inc(ans);
write(ans);
close(input);close(output);
end.

var i,j,l,n:longint;begin read(n);for i:=2to n-1do for j:=i+1to n do if(frac(sqrt(i*i+j*j))=0)and(sqrt(i*i+j*j)<=n)then l:=l+1;write(l)end.

var
i,j,n,k,x:longint;
begin
readln(n);
i:=1;
j:=1;
k:=0;
x:=1;
while i<n do
 begin
  while (j<=i) do
   begin
    x:=i*i+j*j;
    if (frac(sqrt(x))=0) and (trunc(sqrt(x))<=n) then k:=k+1;
    j:=j+1;
   end;
  i:=i+1;
  j:=1;
 end;
writeln(k);
end.

const finp='';
      fout='';
var n:integer;
procedure nhap;
var f:Text;
begin
    assign(f,finp);
    reset(f);
    read(f,n);
    close(f);
end;
function dem:integer;
var i,j,d:integer;
begin
    d:=0;
    for i:=1 to n-1 do
        for j:=i+1 to n do
            if (sqrt(sqr(i)+sqr(j))<=n) and (frac(sqrt(sqr(i)+sqr(j)))=0) then
                inc(d);
    dem:=d;
end;
procedure xuat;
var f:Text;
begin
    assign(f,fout);
    rewrite(f);
    write(f,dem);
    close(f);
end;
begin
    nhap;
    xuat;
end.

var
    i, j, k, x, y, z, m, N : longint;
    ans: longint;

function gcd(p, q : longint) : longint;
begin
    if q = 0 then exit(p);
    exit(gcd(q, p mod q));
end;

begin
  readln(N);
  m := trunc(sqrt(N));
  if m*m < N then inc(m);
  ans := 0;
  for i := 1 to m do
  begin
    if n - i*i > i-1 then j := i-1
    else j := n - i*i;
    for k := 1 to j do
    begin
      x := i*i - k*k;
      y := 2*i*k;
      z := i*i + k*k;
      if (sqr(x) + sqr(y) = sqr(z)) and (z <= N) and (gcd(x, y) = 1) and (gcd(y, z) = 1) then inc(ans, N div z);
    end;
  end;
  writeln(ans);
end.

var a, b, c, d, k, ans, n : longint;

begin
read(n);
ans := 0;
for a := 1 to n do begin
c := a;
d := a * a;
for b := a to n do begin
k := d + b * b;
while (c <= n) and (c * c < k) do inc(c);
if (c > n) then break;
if (c * c = k) then inc(ans);
end;
end;
writeln(ans);

end.

var
  n, i, j, sumsq, out : longint;
  sq : double;
begin
  read(n);
  out := 0;
  for i := 2 to n do
    for j := 1 to i do
    begin
      sumsq := i*i + j*j;
      sq := sqrt(sumsq);
      if (sq - round(sq) = 0 )and( sq <= n) then
        out := out + 1;
    end;
  writeln (out);

end.

var
 i,j: longint;
 n,r,ans,c: longint;
begin
//assign (input, 'input.txt'); reset (input);
readln (n); ans:= 0;
for i:= 1 to n do
 for j:= i+1 to n do
  begin

  c:= round(sqrt (i*i+j*j));
  if c>n then break;
  if c*c=i*i+j*j then inc (ans);

  end;
  writeln (ans);
//close (input);
end.

program aaaaaa;

var n,i,j:longint;
   t,c,n1:int64;

function gcd(x,y:longint):longint;

var z:longint;

begin
while y>0 do
   begin
   z:=x mod y;
   x:=y;
   y:=z;
   end;
gcd:=x;
end;

begin
readln(n);
t:=0;
n1:=trunc(sqrt(n))+1;
for i:=1 to n1 do
   begin
   for j:=1 to i-1 do
      begin
      c:=j*j+i*i;
      if (c<=n) and (gcd(c,i*i-j*j)=1) then
         t:=t+(n div c);
      end;
   end;
writeln(t);
end.

Program taskone;
const nmax = 10000;
var i, j, n, res : longint;
r : extended;
begin
	Readln(n);
	res:=0;
	for i:=1 to nmax do begin
		for j:=i to nmax do begin
			r:= i*i + j*j;
			r:=sqrt(r);
			if r>n then break;
			if frac(r) = 0 then inc(res);
		end;
	end;
	Writeln(res);
end.

{$APPTYPE CONSOLE}
{R+,S+,Q+,I+,O-}
{$R-,S-,Q-,I-,O+}
uses
  SysUtils;
const  latin='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
       glas='AEIOUY';
       cifr='1234567890';

var   code,i,j,nn,mm,lp,rp,tek,ii,jj,n,cnt,min,step,max,new,m : integer;
      k : extended;

begin
{$IFNDEF ONLINE_JUDGE} assign(input,'input.txt'); reset(input);
assign(output,'output.txt'); rewrite(output); {$ENDIF}
     readln(n); cnt:=0;
       for i:=1 to n do
          for j:=i to n do begin
              k:=sqrt(sqr(i)+sqr(j));
              if (frac(k)<0.00001)and(k>=j)and(k<=n)and(k<i+j)and(i<j+k)and(j<i+k) then inc(cnt);
          end;

       writeln(cnt);

{$IFNDEF ONLINE_JUDGE} close(input); close(output); {$ENDIF}
end.

program aaaaaa;

var n,i,x,c,a,b,d:longint;

begin
readln(n);
x:=0;
for i:=1 to n do
   begin
   c:=i*i;
   a:=1;
   while 2*a*a<=c do
      begin
      d:=c-a*a;
      b:=trunc(sqrt(d));
      if b*b=d then
         x:=x+1;
      a:=a+1;
      end;
   end;
writeln(x);
end.

var
  n,i,j,x,s:longint;
  a:real;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to i-1 do
      begin
        x:=sqr(i)-sqr(j);
        a:=sqrt(x);
        if (a=trunc(a)) and (a<=j) then inc(s);
      end;
  writeln(s);
end.

var     n,i,j,k,res : longint;
        a  : array[0..10000] of longint;
        dd : array[0..200000000] of boolean;

begin
        readln(n);
        res := 0;
        j := 1;
        a[1] := 1;
        fillchar(dd,sizeof(dd),false);
        dd[a[1]] := true;
        for i := 2 to n do begin
                inc(j);
                a[j] := i*i;
                dd[a[j]] := true;
        end;
        for i := 1 to n-1 do
                for j := i+1 to n do
                        if dd[a[i] + a[j]] then inc(res);
        writeln(res);
end.

var a,b,c,n, x: LongInt;
begin
  Readln(n);
  x:=0;

  for c:=1 to n do
  for b:=1 to c do
  begin
    a:=trunc( sqrt(c*c-b*b) );
    if (sqr(a)+sqr(b)=sqr(c)) and (a+b>c) and (a+c>b) and (b+c>a) then inc(x);
  end;

  Writeln(x div 2);
end.

var f1,f2:text;n,s:integer;
procedure rd;
begin
        assign(f1,'');reset(f1);
        assign(f2,'');rewrite(f2);
        readln(f1,n);
        close(f1);
end;
procedure wk;
var i,j:integer;d:real;
begin
        s:=0;
        for i:=1 to n-1 do
        for j:=i+1 to n-1 do
        begin
                d:=sqrt(i*i+j*j);
                if (d<=n)and(d-round(d)=0) then inc(s);
        end;
end;
procedure et;
begin
        write(f2,s);
        close(f2);
end;
begin
        rd;
        wk;
        et;
end.

Var     a, b, n, i, j, k : longint;
        dem : int64;
Begin
        readln(n);
        k := n*n;
        for i:=1 to n-2 do begin
                a := i*i;
                for j:=i+1 to n-1 do begin
                        b := j*j;
                        if a+b > k then continue;
                        if frac(sqrt(a+b)) = 0 then dem := dem+1;
                end;
        end;
        writeln(dem);
End.

var
 a,b:longint;
 c:real;
 n,j,i,k:longint;
begin
 readln(n);
 begin
 k:=0;
 for b:=n downto 2 do
   for a:=b-1 downto 1 do
    begin
     c:=sqrt(a*a+b*b);
     if (trunc(c)=c) and (a+b>c) and (c<=n)
          then k:=k+1;
    end;
 writeln(k);
 end;
end.

uses math,sysutils;
var r,kol,g,xv,l,d,res,min1,z,j,max1,l1:integer;x3,y3,x1,y1,x2,y2,x,y,i,k:integer;
    ch:char;s,s1,ss:string;a,e,n,e1,b,c:integer;u:boolean;
    v,v1:real;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n);
  e:=trunc(sqrt(n));
  kol:=0;
  for i:=1 to n do
     for z:=i to 100000 do
        if sqrt(i*i+z*z)>n
        then break
        else Begin
                v:=sqrt(i*i+z*z);
                if v<>round(v)
                then continue;
                inc(kol);
             end;
  writeln(kol);
end.

var b,c,i,j,n,x:longint;
begin
 x:=0;
 read(n);
  for i:=1 to n do
   begin
    c:=sqr(i);
     for j:=1 to i do
      begin
       b:=sqr(j);
        if (c<>b) and (sqrt(c-b)=trunc(sqrt(c-b))) and (sqrt(c-b)<=j) then
         inc(x);
      end;
   end;
  write(x);
end.

var i,j,l,n:longint;begin read(n);for i:=2to n-1do for j:=i+1to n do if(frac(sqrt(i*i+j*j))=0)and(sqrt(i*i+j*j)<=n)then l:=l+1;write(l)end.

VAR     n,i,j,c,k,h:longint;
PROCEDURE   deal;
BEGIN
        c:=0;
        for i:=1 to n do
        for j:=i to n do
        begin
                h:=i*i+j*j;
                if h<=n*n then
                begin
                k:=trunc(sqrt(h));
                if k*k=h then
                   c:=c+1;
                end;
        end;
END;

BEGIN
        Readln(n);
        deal;
        writeln(c);
END.

Uses Math,SysUtils;
Var I,J,L,H,R,G,X,Y,DX,DY,H1,ma,mi:Integer;P1,N,M:Int64;mt,ms:array['0'..'9'] of Integer;R1,B,K,C,D:Extended;F:array[1..100] of Boolean;S,T:String;m1:array[1..100] of Char;p:array[1..1000,1..1000] of char;a:char;
Begin
   ///reset(input,'Input.txt');
   ///assign(output,'Output.txt');
   readln(N);
   R:=0;
   For I:=1 to N do
      For J:=I to N do
      begin
         K:=Round(sqrt(I*I+J*J));
         If (K<=N) and (K*K=I*I+J*J)
         then Inc(R);
      end;
   writeln(R);
end.

const   fi = '';
        fo = '';
var     f,g:text;
        n:longint;
procedure openfile;
begin
        assign(f,fi);reset(F);
        assign(g,fo);rewrite(g);
end;
procedure readfile;
begin
        readln(f,n);
end;
procedure process;
var     i,j,dem,t:longint;
begin
        dem:=0;
        for i:=1 to n do
                for j:=i to n do
                        begin
                                t:=sqr(i)+sqr(j);
                                if sqrt(t) <=n then
                                if trunc(sqrt(t))=sqrt(t) then
                                        begin
                                                inc(dem);
                                        end
                                else
                                else break;
                        end;
        write(g,dem);
end;
procedure closefile;
begin
        close(f);
        close(g);
end;
begin
        openfile;
        readfile;
        process;
        closefile;
end.

var i,j,n,r:integer;
begin
readln(n);
for i:=1 to n do
    for j:=i to n do
        if ((frac(sqrt(i*i+j*j))=0) and ((sqrt(i*i+j*j))<=n)) then r:=r+1;
writeln(r);
readln;
end.

var n,m,i,k,ko : int64; b : array [1..100000000] of boolean; j,l : longint;
    c : array [1..100000] of int64;
function isQ (x : int64) : boolean;
var  d : real;
begin
    b[x]:=true;
    ko:=ko+1;
    c[ko]:=x;
    isq:=true;
end;
begin
  readln(n);
  k:=0;
  ko:=0;
  for l:=1 to n do
  begin
    isq(l*l);
    i:=l*l;
      for j:=1 to ko-1 do if b[i-c[j]] then
      begin
        k:=k+1;
      end;
  end;
  writeln(k div 2);
end.

program z1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  c,a,b,k,n: longint;

begin
//  assign(input,'input.txt'); reset(input);
//  assign(output,'output.txt'); rewrite(output);
 readln(n);
  k:=0;
  for c:=n downto 1 do
   for a:=c-1 downto 1 do
    begin
      b:=trunc(sqrt(c*c-a*a));
      if (b=0)or(b>a) then break;
      if c*c=a*a+b*b then inc(k);
    end;

  writeln(k);
end.

const fi='';
      fo='';
var n:integer;
    kq:longint;

procedure doc;
var f:text;
begin
assign(f,fi);reset(f);
read(f,n);
close(f);
end;

procedure xuly;
var a,c:integer;
    b:longint;
begin
kq:=0;
for c:=1 to n do
        for a:=1 to c do
                begin
                b:=c*c-a*a;
                if (round(sqrt(b))=sqrt(b))and(b>a*a) then inc(kq);
                end;
end;

procedure ghi;
var f:text;
begin
assign(f,fo);rewrite(f);
write(f,kq);
close(f);
end;

BEGIN
doc;xuly;ghi;
END.

var n,i,j,v,k:longint;
begin
    readln(n);
    for i:=1 to n do
     for j:=i to n do
      begin
       v:=trunc(sqrt(sqr(i)+sqr(j)));
       if (v<=n)and(sqr(v)=sqr(i)+sqr(j))then inc(k);
      end;
    writeln(k);
end.

var n,a,b,c,k,c1: longint;

begin
{  assign(input,'input.txt'); reset(input);
  assign(output,'output.txt'); rewrite(output);
}
    read(n);
    k:=0;
    for a:=1 to n-1 do
      for b:=a+1 to n do
        begin
          c:=a*a + b*b; c1:=trunc(sqrt(c));
          if (c1<=n) and (sqr(c1)=c) then inc(k)
        end;

    writeln(k);
end.

// 304 A

var
	a,c,n,ans : longint;

begin
	ans := 0;
	readln(n);
		for c:=n downto 5 do
			for a:=3 to c-1 do
				if(frac(sqrt(c*c-a*a))=0) then
					inc(ans);
	writeln(ans div 2);
end.

var a:array[1..10000] of longint;
    n, ii, i, j, kol:longint;
    b:real;
begin
readln(n);
j:=2; i:=1;
while j<=n do
 begin
 a[i]:=j*j;
 inc(i); inc(j);
 end;
dec(j);dec(j);  kol:=0;
for i:=1 to j-1 do
 begin
  for ii:=i+1 to j do
  begin
  b:=sqrt(a[i]+a[ii]);
  if (frac(b)=0)and(b<=n)
   then inc(kol);
  end;
 end;

writeln(kol);
end.

var
i,j,l,n:integer;
begin
readln(n);
l:=0;
for i:=2 to n-1 do
 for j:=i+1 to n do
 if  (frac(sqrt(i*i+j*j))=0)and (sqrt(i*i+j*j)<=n)  then
 l:=l+1;
 writeln(l);
end.

var
  a,b,c,n,ans:longint;

begin
  readln(n);
  for a:=1 to n do
    for b:=a to n do
      if (sqrt(a*a+b*b)=trunc(sqrt(a*a+b*b))) and (sqrt(a*a+b*b)<=n) then inc(ans);
  writeln(ans);
end.

                          {$APPTYPE CONSOLE}
const  besk=1000000000;
   nnn=2000000;  eps=1e-13;
 label 1,2,3,4;
var n1,n,m,dl,kol,ii,jj,i3,i2,i1,j1,k1,p1,p2,kt,ppp,god,day,mon,mask,i0,j0,k,step,i,j,start,finish:longint;
cc,dd,t,chisl,znam,ot,aa,bb,x0,y0,ll,rr,d1,d2,rad,yy1,xx1,xx2,q,xx,yy,t1,t2,p,modul,pp,tt,ot1,ot2,s,kold,kkr:int64;
kolnul,minus,plus,kk,centr,koef:int64;
flag2,flag1,flag:boolean;
minotkl,dl1,dl2,dl3,hh,po,pt,doltek,dolpred:extended;
b,a,c:array[-10..2000000] of int64;
x1,y1,z1,z,gr,x,y: array[-10..50000] of double;
zan: array[-10..110000000] of boolean;
ee,ww,nn,marked:array[-100000..250000] of int64;
{ss:array[0..2000000] of char; }
sss:string;
ch11,ch12,ch1,ch:char;
sl,sr,stoim,str :array[-10 ..200000] of int64;
ssot:array[-1000..1100000] of char;
fact:array[0..100] of extended;
name:array[0..2000] of string;
function min (a,b:int64):int64;begin min:=b;if a<b then min:=a;  end;
function max (a,b:int64):int64;begin max:=b;if a>b then max:=a;  end;

    function nod(a,b:int64):int64; var p,t:int64;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

function deg(a,k:int64):int64; var p:int64;
begin
 if k=0 then deg:=1 else if odd(k) then deg:=deg(a,k-1)*a mod modul else
   begin  p:=deg(a,k div 2); deg:=p*p mod modul;   end;

end;

function summ(n,p:int64):int64; var i,j,k:int64;
begin
   k:=((n-1) div p)*p;
   summ:=(k div p*(k+p) div 2) mod modul;
end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }
                              modul:=1000000007 ;
 read(n);
for i:=1 to 10000 do zan[i*i]:=true;
for i:=1 to n do for j:=1 to i-1 do if zan[i*i-j*j] then inc(ot);

writeln(ot div 2);

   1:          close(output);
end.

var a,c,ans,n:longint;

begin
    read(n); ans:=0;
	for c:=n downto 2 do
	begin
		a:=1;
		while (2*a*a<=c*c) do
		begin
			if ( frac(sqrt(c*c-a*a))=0 ) then ans:=ans+1;
			a:=a+1;
		end;
	end;
    write(ans);
end.

Var n,a,b,c,i,j,k:LongInt;
  Begin
   Read(n);
    For a:=1 to n do
     For b:=a  to n do
     Begin
      if (Frac(sqrt(sqr(a)+sqr(b)))=0) and (Trunc(sqrt(sqr(a)+sqr(b)))<=n) then k:=k+1;
     End;
   WriteLn(k);
  End.

const   fi='';
        fo='';
var     f,g     :text;
        n,a,b,dem,s:longint;
begin
        assign(g,fo);rewrite(g);
        assign(f,fi);reset(f);
        readln(f,n);
        dem:=0;
        for a:=1 to n do
                for b:=a to n do
                        begin
                                s:=a*a+b*b;
                                if (sqr(trunc(sqrt(s)))=s) and (s<=n*n) then inc(dem);
                        end;
        writeln(g,dem);
        close(f);close(g);
end.

var
 n,i,j,k : longint;
begin
 readln(n);
 for i:=1 to n do
  for j:=i to n do
   if (sqrt(sqr(i)+sqr(j)) <= n) and (frac(sqrt(sqr(i)+sqr(j))) = 0)
    then inc(k);
 writeln(k);
end.

uses math;

var a, b, n, res: longint;
    c: real;

begin
  readln(n);
  res := 0;
  for a := 1 to n do
    for b := a to n do
    begin
      c := sqrt(a*a + b*b);
      if ((c = floor(c)) and (c <= n)) then
        inc(res);
    end;
  writeln(res);
end.

program ProjectA;

{$APPTYPE CONSOLE}

var
  n, i, j, q : integer;
  res : integer;
begin
  readln(n);
  i := 1;
  res := 0;
  while (i <= n) do begin
    j := i;
    while (j <= n) do begin
      q := round(sqrt(i * i + j * j + 0.0));
      if (q * q = i * i + j * j) and (q <= n) then
        inc(res);
      inc(j);
    end;
    inc(i);
  end;
  writelN(res);
  readln;
  readln;
end.

program pytagooo;
var
    n,i,j,temp1,res:longint;
    temp:real;
begin
    read(n);
    for i:=1 to n-1 do
        begin
            for j:=i+1 to n do
                begin
                    temp:=sqrt(sqr(i)+sqr(j));
                    temp1:=trunc(temp);
                    if((temp-temp1=0)and(temp<=n))then inc(res);

                end;
        end;
    writeln(res);
end.

Var n: integer;
    i,j,count: longint;
    k: real;
Begin
        readln(n);
        count:=0;
        For i:=1 to n do
                For j:=i to n do
                        Begin
                                k:= sqrt(i*i+j*j);
                                If ((round(k) = k) and (k<=n)) then inc(count);
                        End;
        Writeln(count);
End.

const p: array[0..10] of longint = (0, 881, 1981, 3172, 4416, 5681, 7008, 8345, 9706, 11077, 12471);
var n,c,a,s,m: longint; b: real;
begin
  readln(n);
  s:=0;
  m:=(n div 1000)*1000+1;
  for c:=m to n do
    for a:=1 to c-1 do
    begin
      b:=sqrt(c*c-a*a);
      if trunc(b)=b then inc(s)
    end;
  write(s div 2+p[n div 1000])
end.

program p1;
var c:real;a,b,k,n:longint;
begin
readln(n);
for a:=1 to n do
for b:=1 to a-1 do
begin
c:=sqrt(a*a-b*b);
if ((c=round(c)) and (c>b)) then k:=k+1;
end;
writeln(k);
end.

Var  i,j,n,ans:longInt;
  begin
    Read(n);
    For i:=1 to n do
     begin
       For j:=1 to n do
        begin
          if (Frac(Sqrt(i*i+j*j))=0)  and (trunc(Sqrt(i*i+j*j))<=n) then inc(ans);
        end;
     end;
    WriteLn(ans div 2);
  end.

var
  n,i,j,x,s:longint;
  a:real;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to i-1 do
      begin
        x:=sqr(i)-sqr(j);
        a:=sqrt(x);
        if (a=trunc(a)) and (a<=j) then inc(s);
      end;
  writeln(s);
end.

var b,c,i,j,n,x:longint;
begin
 x:=0;
 read(n);
  for i:=1 to n do
   begin
    c:=sqr(i);
     for j:=1 to i do
      begin
       b:=sqr(j);
        if (c<>b) and (sqrt(c-b)=trunc(sqrt(c-b))) and (sqrt(c-b)<=j) then
         inc(x);
      end;
   end;
  write(x);
end.

var n,i,j,k,dem:longint;
begin
readln(n);
dem:=0;
for i:=5 to n do
for j:=1 to i - 1 do
begin
    k:=i * i - j * j;
    if k < j * j then break;
    if trunc(sqrt(k)) = sqrt(k) then inc(dem);
end;
write(dem);
end.

var
  n,i,j : longint;
  t,t1 : extended;
  ans : longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=i+1 to n do
    begin
      t:=sqrt((i*i)+(j*j));
      t1:=trunc(t);
      if (t=t1) and (t<=n) then inc(ans);
    end;
  writeln(ans);
end.

var
i,m,j,n,a,b,c,k:integer;
begin
readln(n);
for i:= 1 to n do
for j:= i+1 to n do
begin
k:=sqr(i)+sqr(j);
c:=trunc(sqrt(k));
if (sqr(c)=k) and (c<=n) then
m:=m+1;
end;
writeln(m);
end.

var i,j,l,n:longint;begin read(n);for i:=2to n-1do for j:=i+1to n do if(frac(sqrt(i*i+j*j))=0)and(sqrt(i*i+j*j)<=n)then l:=l+1;write(l)end.

var a, b, c, d, k, ans, n : longint;

begin
    read(n);
    ans := 0;
    for a := 1 to n do begin
        c := a;
        d := a * a;
        for b := 1 to n do begin
            k := d + b * b;
            while (c * c < k) do inc(c);
            if (c <= n) and (c * c = k) and (a <= b) then inc(ans);
        end;
    end;
    writeln(ans);

end.

{$r-,s-,q-,i-,o+}
var
  n,i,j,ans:longint;
begin
  readln(n);
  ans:=0;
  for i:=1 to n do
    for j:=i to n do
      if (sqrt(i*i+j*j)=trunc(sqrt(i*i+j*j))) and
         (sqrt(i*i+j*j)<=n) then inc(ans);
  writeln(ans);
end.

var
n,i,j,ans,t1,t2 : longint;
begin
  readln(n);
  for i:=1 to n do
  for j:=i to n do
   begin
    t1:=sqr(i)+sqr(j);
    t2:=trunc(sqrt(t1));
    if (sqr(t2)=t1) and (t2<=n) then inc(ans);
   end;
  writeln(ans);
end.

var
    i, j, k, x, y, z, m, N : longint;
    ans: longint;

function gcd(p, q : longint) : longint;
begin
    if q = 0 then exit(p);
    exit(gcd(q, p mod q));
end;

begin
  readln(N);
  m := trunc(sqrt(N));
  if m*m < N then inc(m);
  ans := 0;
  for i := 1 to m do
  begin
    if n - i*i > i-1 then j := i-1
    else j := n - i*i;
    for k := 1 to j do
    begin
      x := i*i - k*k;
      y := 2*i*k;
      z := i*i + k*k;
      if gcd(x, y) = 1 then inc(ans, N div z);
    end;
  end;
  writeln(ans);
end.

var a, b, c, d, k, ans, n : longint;

begin
read(n);
ans := 0;
for a := 1 to n do begin
c := a;
d := a * a;
for b := a to n do begin
k := d + b * b;
while c * c < k do inc(c);
if (c * c = k) and (c <= n) then inc(ans);
end;
end;
writeln(ans);

end.

var
	n,t,ans,i,j : longint;
begin
	readln(n);
	ans := 0;
	for i := 1 to n do begin
		for j := i+1 to n do begin
			t := trunc(sqrt(i*i + j*j));
			if (t <= n) and (t*t = i*i + j*j) then
				inc(ans);
		end;
	end;
	writeln(ans);
end.

var
    a, b, c, ans, n:    integer;

begin
    ans := 0;
    readln(n);
    for a := 1 to n do
        for b := a to n do begin
            c := trunc(sqrt(sqr(a) + sqr(b)));
            if (sqr(c) = sqr(a) + sqr(b)) and (c <= n) then
                inc(ans);
        end;
    write(ans);
end.

program EA;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  a, b, n, k : integer;

begin
  readln(n);
  k := 0;
  for a := 1 to n do
    for b := a  to n do
      if (sqrt(sqr(a) + sqr(b)) <= n) and (frac(sqrt(sqr(a) + sqr(b))) = 0) then inc(k);
  writeln(k);
  readln;
end.

program aaaaaa;

var i:longint;
   n,x,c,a,b,d:longint;

begin
readln(n);
x:=0;
for i:=1 to n do
   begin
   c:=i*i;
   a:=1;
   while 2*a*a<=c do
      begin
      d:=c-a*a;
      b:=trunc(sqrt(d));
      if b*b=d then
         x:=x+1;
      a:=a+1;
      end;
   end;
writeln(x);
end.

var n,i,j,k,dem:longint;
begin
readln(n);
dem:=0;
for i:=5 to n do
for j:=1 to i - 1 do
begin
    k:=i * i - j * j;
    if k < j * j then break;
    if trunc(sqrt(k)) = sqrt(k) then inc(dem);
end;
write(dem);
end.

const fi='';
      fo='';
var
      f,g   :text;
      n     :longint;
      a     :array[0..100000] of longint;
      dem,s:longint;
procedure nhap;
begin
      assign(f,fi);reset(f);
      readln(f,n);
      close(f);
end;
procedure xuli;
var   i,j,t :longint;
begin
      dem:=0;
      for i:=1 to n-1 do
            for j:=i to n do
                  begin
                  s:=i*i+j*j;
                  t:=trunc(sqrt(s));
                  if (t*t=s) and(t<=n) then inc(dem);
                  end;
      assign(g,fo);rewrite(g);
      write(g,dem);
      close(g);
end;
BEGIN
      nhap;
      xuli;
end.

var i,j,kol,n: longint;  k: double;

begin
 read(n);
 for i:=1 to 10000 do
  for j:=i to 10000 do
   begin
    k:=sqrt(sqr(i)+sqr(j));
    if k>n then break;
    if frac(k)<0.000001 then kol+=1;
   end;
 write(kol);
end.

var
  n,i,j : longint;
  t,t1 : extended;
  ans : longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=i+1 to n do
    begin
      t:=sqrt((i*i)+(j*j));
      t1:=trunc(t);
      if (t=t1) and (t<=n) then inc(ans);
    end;
  writeln(ans);
end.

var
  n,i,j,c,k,m:integer;
begin
  readln(n);
  m:=0;
for i:=1 to n do
for j:=i+1 to n do
begin
k:=sqr(i)+sqr(j);
c:=trunc(sqrt(k));
if (sqr(c)=k) and (c<=n) then
m:=m+1;
end;
 writeln(m);
end.

var
 i,j,K,Kol,N:longint;
begin
 Read(N);
  For i:=1 to N do
   For j:=i to N do begin
      k:=Trunc(Sqrt(i*i+j*j));
      if (k*k=i*i+j*j)and
         (k>i)and(k>j)and
         (k<=n) then begin
           //                Write(i,' ',j,' ',k);
                           Inc(Kol);
                      //     Writeln;
                          end;
                    end;
 Write(Kol);
end.

var
n,i,j,ans:longint;
begin
read(n);
for i:=1 to n do
  for j:=i+1 to n do
    if (sqr(i)+sqr(j)<=sqr(n)) and (frac(sqrt(sqr(i)+sqr(j)))<0.0000001)then inc(ans);
Writeln(ans);
end.

program amocana;
var i,j,n,k:longint;
    m:real;
begin
read(n);
for i:=1 to n do
for j:=1 to i-1 do
begin
m:=sqrt(i*i-j*j);
if (m=round(m)) and (m>j) then k:=k+1;
end;
writeln(k);
end.

var i,j,l,n:longint;begin read(n);for i:=2to n-1do for j:=i+1to n do if(frac(sqrt(i*i+j*j))=0)and(sqrt(i*i+j*j)<=n)then l:=l+1;write(l)end.

program p1;
var
  sol,i,j,n:longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to trunc(sqrt(i*i/2)) do
     if sqrt(i*i-j*j)=trunc(sqrt(i*i-j*j)) then inc(sol);
  writeln(sol);
end.

var a,c,ans,n:longint;

begin
    read(n); ans:=0;
	for c:=n downto 2 do
	begin
		a:=1;
		while (2*a*a<=c*c) do
		begin
			if ( frac(sqrt(c*c-a*a))=0 ) then ans:=ans+1;
			a:=a+1;
		end;
	end;
    write(ans);
end.

Program a;
Const fi='';
      fo='';
var f1,f2:text;
    sl,n,i,j,c,kq:longint;
Procedure nhap;
begin
     assign(f1,fi);
     assign(f2,fo);
     reset(f1);
     rewrite(f2);
     readln(f1,n);
end;

Procedure xuli;
begin
sl:=0;
     for i:=1 to n do
         for j:=i to n do
             begin
                kq:=i*i+j*j;
                c:=trunc(sqrt(kq));
                if (c*c=kq) and (c<=n) then sl:=sl+1;
             end;
end;

Procedure xuat;
begin
     writeln(f2,sl);
     close(f1);
     close(f2);
end;

begin
nhap;
xuli;
xuat;
end.

{$MODE OBJFPC}
program PythagoreanTheoremII;
const
    fin='P304A.inp';
    fou='P304A.out';

var
    n:longint;
    count:longint=0;

function    check(x,y:longint):boolean;
var
    tmp:longint;
begin
    result:=false;
    tmp:=trunc(sqrt(sqr(x)-sqr(y)));
    if (sqr(tmp)=sqr(x)-sqr(y)) or (sqr(tmp+1)=sqr(x)-sqr(y)) then
        exit(true);
end;

procedure   main();
var
    x,y:longint;
begin
    Read(n);
    for x:=1 to n do
        for y:=1 to x do
            if (check(x,y)) then
                inc(count);
    Write(count div 2);
end;

begin
    main();
end.

    var x,y,z,c:extended;
 i,j,n,a,b:longint;
 begin
  read(n);

  for a:=1 to n do
   for b:=1 to  n do

    begin
     c:=((a*a)+(b*b));
     c:=sqrt(c);
     if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1;

    end;
     write(x:0:0);
 end.

var x,y,z:extended;
 i,j,n,a,b,c:longint;
 begin
  read(n);
     if n<=1000 then
      begin
  for a:=1 to n do
   for b:=a to  n do
     for c:=b to n do
    begin
     if (c*c)=(a*a)+(b*b) then
     if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                 else break;
    end;
     write(x:0:0);
      end
       else
          if n<= 2000 then
        begin
        x:=881;
           for a:=1001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);
        end
         else
            if n<= 3000 then
        begin
        x:=1038;
           for a:=2001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);

      end
       else
          if n<= 4000 then
        begin
        x:=1708;
           for a:=3001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);
        end
         else
            if n<= 5000 then
        begin
        x:=1708;
           for a:=4001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);
      end
       else
         if n<=6000 then
      begin
       x:=1708;
  for a:=5001 to n do
   for b:=a to  n do
     for c:=b to n do
    begin
     if (c*c)=(a*a)+(b*b) then
     if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                 else break;
    end;
     write(x:0:0);
      end
       else
          if n<= 7000 then
        begin
        x:=1723;
           for a:=6001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);
        end
         else
            if n<= 8000 then
        begin
        x:=1723;
           for a:=7001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;
    end;
     write(x:0:0);
      end
       else
       if n<=9000 then
      begin
       x:=1723;
  for a:=8001 to n do
   for b:=a to  n do
     for c:=b to n do
    begin
     if (c*c)=(a*a)+(b*b) then
     if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                 else break;
    end;
     write(x:0:0);
      end
       else
          if n<= 10000 then
        begin
        x:=1723;
           for a:=9001 to n do
            for b:=a to  n do
               for c:=b to n do
                begin
                 if (c*c)=(a*a)+(b*b) then
                    if (1<=a) and (a<=b) and (b<=c) and (c<=n) then x:=x+1
                                                        else break;

     end;
     write(x:0:0);
     end;

 end.

var
 i,j: longint;
 n,r,ans: int64;
 k: real;
begin
//assign (input, 'input.txt'); reset (input);
read (n);
for i:= 1 to n do
 for j:= i+1 to n do
  begin
  r:= i*i+j*j;
  K:= frac (sqrt (r));
  if frac(sqrt (r))=0 then inc (ans);
  if r>n*n then break;
  end;
  writeln (ans);
//close (input);
end.

var
 i,j: longint;
 n,r,ans,c: longint;
begin
//assign (input, 'input.txt'); reset (input);
read (n); ans:= 0;
for i:= 1 to n do
 for j:= i+1 to n do
  begin
  r:= i*i+j*j;
  c:= round(sqrt (r));
  if c*c=r then inc (ans);
  if c>n then break;
  end;
  writeln (ans);
//close (input);
end.

var
 i,j,K,Kol,N:longint;
begin
 Read(N);
  For i:=1 to N do
   For j:=i to N do begin
      k:=Trunc(Sqrt(i*i+j*j));
      if k*k=i*i+j*j then Inc(Kol);
                    end;
 Write(Kol);
end.

var
 a,b:longint;
 c:real;
 n,j,i,k:longint;
begin
 readln(n);
 begin
 k:=0;
 for b:=n-1 downto 2 do
   for a:=b-1 downto 2 do
    begin
     c:=sqrt(a*a+b*b);
     if (trunc(c)=c) and (a+b>c)
          then k:=k+1;
    end;
 writeln(k);
 end;
end.

Var i,j,n,rez:longint;
function proverka(a:longint):boolean;
Var e:longint;
begin
  e:=round(sqrt(a));
  if (e*e<>a)or(e>n)
  then proverka:=false
  else proverka:=true;
end;
begin
//  Assign(input,'input.txt'); reset(input);
//  Assign(output,'output.txt'); rewrite(output);
  rez:=0;
  read(n);
  for i:=1 to 100 do
  for j:=i to 100 do
  if proverka(i*i+j*j)
  then inc(rez);
  writeln(rez);
end.

var a,c,ans,n:longint;

begin
    read(n);
    c:=trunc(sqrt(n));
    a:=1;
    while (2*a*a<c*c) do
    begin
        if ( frac(sqrt(c*c-a*a))=0 ) then ans:=ans+1;
        a:=a+1;
    end;
    write(ans);
end.

Program taskone;
const nmax = 8000;
var i, j, n, res : longint;
r : extended;
begin
	Readln(n);
	res:=0;
	for i:=1 to nmax do begin
		for j:=i to nmax do begin
			r:= i*i + j*j;
			r:=sqrt(r);
			if r>n then break;
			if frac(r) = 0 then inc(res);
		end;
	end;
	Writeln(res);
end.

var
  ans,n,a,b:longint;
  c:double;

begin
  readln(n);
  for a:=1 to n do
    for b:=a+1 to n do
       begin
       c:=sqrt(sqr(a)+sqr(b));
       if c-trunc(c)<0.00000000001 then
         inc(ans);
       end;
   writeln(ans);
  end.

var
  n,i,j : longint;
  t,t1 : extended;
  ans : longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=i to n do
    begin
      t:=sqrt((i*i)+(j*j));
      t1:=trunc(t);
      if (t=t1) then inc(ans);
    end;
  writeln(ans);
end.

var i,j,kol,n: longint;  k:real;

begin
 read(n);
 for i:=1 to 1000 do
  for j:=i to 1000 do
   begin
    k:=sqrt(sqr(i)+sqr(j));
    if sqr(k)=sqr(trunc(k)) then
    if (sqr(i)+sqr(j)<=sqr(n)) then kol+=1 else break;
   end;
 write(kol);
end.

var i,j,kol,n: longint;  k: double;

begin
 read(n);
 for i:=1 to 1000 do
  for j:=i to 1000 do
   begin
    k:=sqrt(sqr(i)+sqr(j));
    if frac(k)<0.000001 then
    if (sqr(i)+sqr(j)<=sqr(n)) then kol+=1 else break;
   end;
 write(kol);
end.

var n,i,j,c,ans:longint;
begin
  ans:=0;
  readln(n);
  for i:=1 to 100 do
    for j:=1 to 100 do
    begin
      c:=sqr(i)+sqr(j);
      if (trunc(sqrt(c))<=n) and (sqrt(c)=trunc(sqrt(c))) then inc(ans);
    end;
  writeln(ans div 2);
end.

const   fi = '';
        fo = '';
var     f,g:text;
        n:longint;
procedure openfile;
begin
        assign(f,fi);reset(F);
        assign(g,fo);rewrite(g);
end;
procedure readfile;
begin
        readln(f,n);
end;
procedure process;
var     i,j,dem,t:longint;
begin
        dem:=0;
        for i:=1 to n do
                for j:=i to n do
                        begin
                                t:=sqr(i)+sqr(j);
                                if sqrt(t) <=n then
                                if trunc(sqrt(t))=sqrt(t) then
                                        begin
                                                inc(dem);
                                                writeln(i,' ',j);
                                        end
                                else
                                else break;
                        end;
        write(g,dem);
end;
procedure closefile;
begin
        close(f);
        close(g);
end;
begin
        openfile;
        readfile;
        process;
        closefile;
end.

Var n,a,b,c,i,j,k:LongInt;
  Begin
   Read(n);
    For a:=1 to n do
     For b:=a  to n do
     Begin
      if (Frac(sqrt(sqr(a)+sqr(b)))=0) and (Trunc(sqrt(sqr(a)+sqr(b)))>=n) then k:=k+1;
     End;
   WriteLn(k);
  End.

var
n,i,j,ans,t1,t2 : longint;
begin
  readln(n);
  for i:=1 to n do
  for j:=i to n do
   begin
    t1:=sqr(i)+sqr(j);
    t2:=trunc(sqrt(t1));
    if (sqr(t2)=t1) and (t2>=j) then inc(ans);
   end;
  writeln(ans);
end.

var n,c,a,s: longint;
begin
  readln(n);
  for c:=5 to n do
    for a:=1 to n-1 do if sqrt(c*c-sqr(round(sqrt(c*c-a*a))))=a then
    begin
      inc(s);
      break
    end;
  writeln((s div 2)+(s mod 2))
end.

var n,c,a,s: longint;
begin
  readln(n);
  for c:=4 to n do
    for a:=1 to n-1 do if sqrt(c*c-sqr(round(sqrt(c*c-a*a))))=a then
    begin
      inc(s);
      break
    end;
  writeln(s div 2)
end.

var n,c,a,s: longint;
begin
  readln(n);
  for c:=4 to n do
    for a:=1 to n-1 do if trunc(sqrt(c*c-a*a))=sqrt(c*c-a*a) then
    begin
      inc(s);
      break
    end;
  writeln(s div 2)
end.

program A;

const mang : array [1..16] of integer = (5, 13, 25, 17, 41, 61, 37, 85, 65, 29, 53, 65, 85, 89, 73, 97);

var n : integer;

 procedure nhap;
 var i, j, count : integer;
 Begin
        readln (n);
        count := 0;
        for i := 1 to 16 do
                count := count + n div mang[i];
         writeln (count);
 End;

Begin
        nhap;
End.

var
n,i,j,ans:longint;
begin
read(n);
for i:=1 to n do
  for j:=i to n do
    if (sqr(i)+sqr(j)<=sqr(n)) and (frac(sqrt(sqr(i)+sqr(j)))<0.001)then inc(ans);
Writeln(ans);
end.

var n:integer;
begin
readln(n);
writeln(n div 5);
end.

var a,b,n:integer; s,sl:longint;
begin
    readln(n); sl:=0;
    for a:=1 to n-1 do
    for b:=1 to n-1 do
    begin
        s:=a*a+b*b;
        if ((s<=n) and (trunc(sqrt(s))*trunc(sqrt(s))=s)) then inc(sl);
    end;
    writeln(sl);
end.

program p1;
var c:real;a,b,k,n:longint;
begin
readln(n);
for a:=1 to n do
for b:=1 to a-1 do
begin
c:=sqrt(a*a-b*b);
if ((c=round(c)) and (c>a)) then inc(k);
end;
writeln(k);
end.

var a, b, c, d, k, ans, n : longint;

begin
read(n);
ans := 0;
for a := 1 to n do begin
c := a;
d := a * a;
for b := a to n do begin
k := d + b * b;
while c * c < k do inc(c);
if (c * c = k) then inc(ans);
end;
end;
writeln(ans);

end.

var a, b, c, d, k, ans, n : longint;

begin
    read(n);
    ans := 0;
    for a := 1 to n do begin
        c := a;
        for b := 1 to n do begin
            k := d + b * b;
            while (c <= n) and (c * c < k) do inc(c);
            if (c > n) then break;
            if (c * c = k) and (a <= b) then inc(ans);
        end;
    end;
    writeln(ans);

end.

var a, b, c, d, k, ans, n : longint;

begin
    read(n);
    ans := 0;
    for a := 1 to n do begin
        c := a;
        for b := 1 to n do begin
            k := d + b * b;
            while (c <= n) and (c * c < k) do inc(c);
            if (c > n) then break;
            if (c * c = k) and (a < b) then inc(ans);
        end;
    end;
    writeln(ans);

end.