      SUBROUTINE AD_MAIN ()
        IMPLICIT INTEGER (I-K), REAL (R-T), DOUBLE PRECISION (D-F), COMP
     *LEX (X-Z), CHARACTER (A-C), LOGICAL (L-N)
        
        INTEGER :: K_MA (0:15)
        
        INTEGER, DIMENSION (0:1) :: K_PQ
        INTEGER, DIMENSION (0:35) :: I_A
        REAL, DIMENSION (0:35) :: R_B
        DOUBLE PRECISION, DIMENSION (0:35) :: D_C
        LOGICAL, DIMENSION (0:35) :: L_M
        INTEGER :: I_J
        
        LOGICAL  N_FIRSTCALL
        DATA     N_FIRSTCALL /.TRUE./
        INTEGER  ::  K_Q, K_H0, K_H1, K_H2, K_H3, K_H4, K_H5, K_H6, K_H7
     *, K_P0, K_P1 (0:0), K_P2 (0:1), K_P3 (0:2), K_P4 (0:3), K_P5 (0:4)
     *, K_P6 (0:5), K_P7 (0:6)
        COMMON /Q_PROCS/ K_Q, K_H0, K_H1, K_H2, K_H3, K_H4, K_H5, K_H6, 
     *K_H7, K_P0, K_P1, K_P2, K_P3, K_P4, K_P5, K_P6, K_P7
        
        INTEGER           ::  K_PTR, K_Z, K_TMP (0 : 299999)
        REAL              ::  T_TMP (0 : 299999)
        DOUBLE PRECISION  ::  F_TMP (0 : 149999)
        COMPLEX           ::  Z_TMP (0 : 149999)
        LOGICAL           ::  N_TMP (0 : 299999)
        EQUIVALENCE (K_TMP, T_TMP, F_TMP, Z_TMP, N_TMP)
        COMMON /Q_STACK/ K_PTR, K_Z, K_TMP
        
        K_PTR = 0
        
        CALL AD_INIT_PROCS (K_H1, 1, (/1/))
        CALL AD_PROC_COORD (K_H1, K_P1)
        CALL AD_INIT_PROCS (K_H2, 2, (/1, 1/))
        CALL AD_PROC_COORD (K_H2, K_P2)
        CALL AD_INIT_PROCS (K_H3, 3, (/1, 1, 1/))
        CALL AD_PROC_COORD (K_H3, K_P3)
        CALL AD_INIT_PROCS (K_H4, 4, (/1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H4, K_P4)
        CALL AD_INIT_PROCS (K_H5, 5, (/1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H5, K_P5)
        CALL AD_INIT_PROCS (K_H6, 6, (/1, 1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H6, K_P6)
        CALL AD_INIT_PROCS (K_H7, 7, (/1, 1, 1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H7, K_P7)
        CALL AD_INIT_PROCS (K_HQ, 2, (/2, 3/))
        CALL AD_PROC_COORD (K_HQ, K_PQ)
        
        K_MA = (/73, 0, K_HQ, 0, 80, 1, 36, 15, 80, 1, 100, 6, 4, 1, 0, 
     *0/)
        
        DO 10 I_I = 1, 80
          IF (I_I.LT.13) THEN
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              I_A (K_V0) = I_I
            ENDIF
            
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              R_B (K_V0) = I_I
            ENDIF
            
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              D_C (K_V0) = I_I
            ENDIF
            
          ELSE
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              I_A (K_V0) = 1
            ENDIF
            
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              R_B (K_V0) = 1
            ENDIF
            
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
              K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
              D_C (K_V0) = 1
            ENDIF
            
          ENDIF
          
          IF (K_PQ(0) == 0 .AND. K_PQ(1) == MOD((I_I+14)/6,3)) THEN
            K_V0 = MOD(I_I+14,6)+(I_I+14)/18*6
            L_M (K_V0) = .FALSE.
          ENDIF
          
   10   CONTINUE
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 2) THEN
          L_M (5) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 0) THEN
          L_M (7) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 0) THEN
          L_M (9) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 0) THEN
          L_M (11) = .TRUE.
        ENDIF
        
        CALL AD_MAXVAL_I (K_V0, I_A, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_MAXVAL_R (T_V1, R_B, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_MAXVAL_D (F_V2, D_C, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_MAXVAL_I (K_V0, I_A, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_MAXVAL_R (T_V1, R_B, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_MAXVAL_D (F_V2, D_C, K_MA, L_M, K_MA, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_MINVAL_I (K_V0, I_A, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_MINVAL_R (T_V1, R_B, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_MINVAL_D (F_V2, D_C, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_MINVAL_I (K_V0, I_A, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_MINVAL_R (T_V1, R_B, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_MINVAL_D (F_V2, D_C, K_MA, L_M, K_MA, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_SUM_I (K_V0, I_A, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_SUM_R (T_V1, R_B, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_SUM_D (F_V2, D_C, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_SUM_I (K_V0, I_A, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_SUM_R (T_V1, R_B, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_SUM_D (F_V2, D_C, K_MA, L_M, K_MA, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_PRODUCT_I (K_V0, I_A, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_PRODUCT_R (T_V1, R_B, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        CALL AD_PRODUCT_D (F_V2, D_C, K_MA, N_TMP(0), (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        CALL AD_PRODUCT_I (K_V0, I_A, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_PRODUCT_R (T_V1, R_B, K_MA, L_M, K_MA, .TRUE.)
        
        CALL AD_PRODUCT_D (F_V2, D_C, K_MA, L_M, K_MA, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(i15,2f15.2)", K_V0,T_V1,F_V2
        ENDIF
        K_T0 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M0 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M0 : K_M0+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_I (K_TMP(K_T0), K_TMP(K_M0), I_A, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        K_T1 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M1 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M1 : K_M1+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_R (K_TMP(K_T1), K_TMP(K_M1), R_B, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        K_T2 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M2 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M2 : K_M2+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_D (K_TMP(K_T2), K_TMP(K_M2), D_C, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(3i15)", K_TMP(K_T0:K_T0+(0)),K_TMP(K_T1:K_T1+(0)),K_TM
     *P(K_T2:K_T2+(0))
        ENDIF
        K_PTR = K_PTR - 48
        K_T0 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M0 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M0 : K_M0+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_I (K_TMP(K_T0), K_TMP(K_M0), I_A, K_MA, L_M, K_MA
     *, .TRUE.)
        
        K_T1 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M1 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M1 : K_M1+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_R (K_TMP(K_T1), K_TMP(K_M1), R_B, K_MA, L_M, K_MA
     *, .TRUE.)
        
        K_T2 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M2 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M2 : K_M2+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MAXLOC_D (K_TMP(K_T2), K_TMP(K_M2), D_C, K_MA, L_M, K_MA
     *, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(3i15)", K_TMP(K_T0:K_T0+(0)),K_TMP(K_T1:K_T1+(0)),K_TM
     *P(K_T2:K_T2+(0))
        ENDIF
        K_PTR = K_PTR - 48
        K_T0 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M0 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M0 : K_M0+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_I (K_TMP(K_T0), K_TMP(K_M0), I_A, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        K_T1 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M1 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M1 : K_M1+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_R (K_TMP(K_T1), K_TMP(K_M1), R_B, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        K_T2 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M2 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M2 : K_M2+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_D (K_TMP(K_T2), K_TMP(K_M2), D_C, K_MA, N_TMP(0),
     * (/0/), .FALSE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(3i15)", K_TMP(K_T0:K_T0+(0)),K_TMP(K_T1:K_T1+(0)),K_TM
     *P(K_T2:K_T2+(0))
        ENDIF
        K_PTR = K_PTR - 48
        K_T0 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M0 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M0 : K_M0+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_I (K_TMP(K_T0), K_TMP(K_M0), I_A, K_MA, L_M, K_MA
     *, .TRUE.)
        
        K_T1 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M1 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M1 : K_M1+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_R (K_TMP(K_T1), K_TMP(K_M1), R_B, K_MA, L_M, K_MA
     *, .TRUE.)
        
        K_T2 = K_PTR;   K_PTR = K_PTR + 2
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M2 = K_PTR;   K_PTR = K_PTR + 14
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M2 : K_M2+13) = (/129, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
     * 1, -1/)
        
        CALL AD_MINLOC_D (K_TMP(K_T2), K_TMP(K_M2), D_C, K_MA, L_M, K_MA
     *, .TRUE.)
        
        IF (K_P0 == 0) THEN
          PRINT "(3i15)", K_TMP(K_T0:K_T0+(0)),K_TMP(K_T1:K_T1+(0)),K_TM
     *P(K_T2:K_T2+(0))
        ENDIF
        K_PTR = K_PTR - 48
        CALL AD_DELETE_PROCS (K_HQ)
        
        CALL AD_DELETE_PROCS (K_H7)
        CALL AD_DELETE_PROCS (K_H6)
        CALL AD_DELETE_PROCS (K_H5)
        CALL AD_DELETE_PROCS (K_H4)
        CALL AD_DELETE_PROCS (K_H3)
        CALL AD_DELETE_PROCS (K_H2)
        CALL AD_DELETE_PROCS (K_H1)
        CALL AD_DELETE_PROCS (K_H0)
        
      END
