reserve n,n1,n2,k,D for Nat,
        r,r1,r2 for Real,
        x,y for Integer;

theorem Th13:
  D is non square implies
    ex k,a,b,c,d be Integer st
       0 <> k & a^2 - D*b^2 = k = c^2 -D*d^2 &
       a,c are_congruent_mod k & b,d are_congruent_mod k &
    (|.a.| <> |.c.| or |.b.| <> |.d.|)
  proof
    set S={[x,y] where x is Integer, y is Integer:
    y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 & 0 < x-y*(sqrt D)};
    assume A1: D is non square;
    sqrt D >=0 by SQUARE_1:def 2;
    then reconsider M = [/2*(sqrt D)+1 \] as Element of NAT by INT_1:53;
    defpred P[object,object] means
    for x,y be Integer st [x,y]= $1 holds $2 = x^2 - D*y^2;
    A2:for xy be object st xy in S ex u be object st P[xy,u]
    proof
      let xy be object such that A3: xy in S;
      consider x,y be Integer such that
      A4: xy=[x,y] & y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 &
      0 < x-y*(sqrt D) by A3;
      take u= x^2 - D*y^2;
      let x1,y1 be Integer such that A5: [x1,y1] = xy;
      x1=x & y1=y by A4,A5,XTUPLE_0:1;
      hence thesis;
    end;
    consider f be Function such that
    A6:  dom f = S & for xy be object st xy in S holds P[xy,f.xy]
       from CLASSES1:sch 1(A2);
    sqrt D >=0 by SQUARE_1:def 2;
    then reconsider M = [/2*(sqrt D)+1 \] as Element of NAT by INT_1:53;
    defpred P[Integer] means $1<>0;
    deffunc F(set)=$1;
    set SS={F(i) where i is Element of INT: -M <= i & i <= M & P[i]};
    SS is finite from XXREAL_2:sch 1;
    then reconsider SS as finite set;
    A7: rng f c= SS
    proof
      let r be object;assume r in rng f;
      then consider xy be object such that
      A8: xy in dom f & f.xy=r by FUNCT_1:def 3;
      consider x,y be Integer such that
      A9: xy=[x,y] & y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 &
      0 < x-y*(sqrt D) by A8,A6;
      A10: f.xy = x^2 - D*y^2 by A8,A9,A6;
      then reconsider r as Element of INT by A8,INT_1:def 2;
      A11: P[r] by A8,A9,A1,A10;
      2*(sqrt D)+1 <= M by INT_1:def 7;
      then |.r.| < M by A8,A9,A10,XXREAL_0:2;
      then -M <= r <= M by ABSVALUE:5;
      hence thesis by A11;
    end;
    then consider k1 be object such that
    A12:k1 in rng f & f"{k1} is infinite by A1,Th12,A6, CARD_2:101;
    k1 in SS by A12,A7;
    then consider k be Element of INT such that
    A13:  k=k1 & -M <= k <= M & P[k];
    set Z= f"{k};
    take k;
    A14: Z c= S by RELAT_1:132,A6;
    defpred R[object,object] means
    for x,y be Integer st [x,y]= $1 holds $2 = [x mod k,y mod k];
    A15:for xy be object st xy in Z ex u be object st R[xy,u]
    proof
      let xy be object such that A16: xy in Z;
      xy in S by A16,A14;
      then consider x,y be Integer such that
      A17: xy=[x,y] & y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 &
      0 < x-y*(sqrt D);
      take u=[x mod k,y mod k];
      let x1,y1 be Integer such that A18: [x1,y1] = xy;
      x1=x & y1=y by A17,A18,XTUPLE_0:1;
      hence thesis;
    end;
    consider g be Function such that
    A19:dom g = Z & for xy be object st xy in Z holds R[xy,g.xy]
    from CLASSES1:sch 1(A15);
    defpred R[object] means not contradiction;
    set K ={F(i) where i is Element of INT: -|.k.| <= i & i <= |.k.| & R[i]};
    A20: K is finite from XXREAL_2:sch 1;
    rng g c= [:K,K:]
    proof
      let b be object;
      assume b in rng g;
      then consider a be object such that
      A21:a in dom g & g.a = b by FUNCT_1:def 3;
      a in dom f & f.a in {k} by A19,A21,FUNCT_1:def 7;
      then consider x,y be Integer such that
      A22: a=[x,y] & y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 &
      0 < x-y*(sqrt D) by A6;
      A23:g.a = [x mod k,y mod k] by A22,A21,A19;
      A24: x mod k in INT by INT_1:def 2;
      |. x mod k .| < |.k.| by Th2,A13;
      then -|.k.| <= x mod k <= |.k.| by ABSVALUE:5;
      then A25: x mod k in K by A24;
      A26: y mod k in INT by INT_1:def 2;
      |. y mod k .| < |.k.| by Th2,A13;
      then -|.k.| <= y mod k <= |.k.| by ABSVALUE:5;
      then y mod k in K by A26;
      hence b in [:K,K:] by A21,A23,A25,ZFMISC_1:87;
    end;
    then consider ab be object such that
    A27:ab in rng g & g"{ab} is infinite by A19,A12,A13, A20,CARD_2:101;
    consider X be object such that
    A28:X in g"{ab} by A27,XBOOLE_0:def 1;
    A29: X in dom g & g.X in {ab} by A28,FUNCT_1:def 7;
    A30: X in dom f & f.X in {k} by A29,A19,FUNCT_1:def 7;
    then consider x,y be Integer such that
    A31: X=[x,y] & y<>0 & |. x^2 - D*y^2.| < 2*(sqrt D)+1 &
    0 < x-y*(sqrt D) by A6;
    A32:g.X = [x mod k,y mod k] by A31,A29,A19;
    ex a,b,c,d be Integer st
    a^2 -D*b^2 = k = c^2 -D*d^2 &
    a,c are_congruent_mod k & b,d are_congruent_mod k &
    (|.a.| <> |.c.| or |.b.| <> |.d.|)
    proof
      assume A33:for a,b,c,d be Integer st a^2 -D*b^2 = k = c^2 -D*d^2 &
      a,c are_congruent_mod k & b,d are_congruent_mod k holds
      |.a.| = |.c.| & |.b.| = |.d.|;
      g"{ab} c= {[x,y],[-x,y],[x,-y],[-x,-y]}
      proof
        let Y be object;assume Y in g"{ab};
        then A34: Y in dom g & g.Y in {ab} by FUNCT_1:def 7;
        then A35: Y in dom f & f.Y in {k} by A19,FUNCT_1:def 7;
        then consider x1,y1 be Integer such that
        A36: Y=[x1,y1] & y1<>0 & |. x1^2 - D*y1^2.| < 2*(sqrt D)+1
        & 0 < x1-y1*(sqrt D) by A6;
        A37:g.X = ab = g.Y by A29,A34,TARSKI:def 1;
        g.Y = [x1 mod k,y1 mod k] by A36,A34,A19;
        then A38: x mod k = x1 mod k & y mod k = y1 mod k
        by A32,A37,XTUPLE_0:1;
        A39:x,x1 are_congruent_mod k
        proof
          x = (x div k) * k + (x mod k) & x1 = (x1 div k) * k + (x1 mod k)
          by INT_1:59,A13;
          then x-x1 = k *((x div k)-(x1 div k)) by A38;
          hence thesis by INT_1:def 5;
        end;
        A40: y,y1 are_congruent_mod k
        proof
          y = (y div k) * k + (y mod k) & y1 = (y1 div k) * k + (y1 mod k)
          by INT_1:59,A13;
          then y-y1 = k *((y div k)-(y1 div k)) by A38;
          hence thesis by INT_1:def 5;
        end;
        f.X = k = f.Y by A30,A35,TARSKI:def 1;
        then  x1^2 - D*y1^2 = k = x^2 - D*y^2 by A36,A35,A30,A31,A6;
        then |.x.|=|.x1.| & |.y.| = |.y1.| by A33,A39,A40;
        then (x=x1 or x=-x1) & (y=y1 or y=-y1) by ABSVALUE:28;
        hence thesis by A36,ENUMSET1:def 2;
      end;
      hence contradiction by A27;
    end;
    hence thesis by A13;
  end;
