      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_MM (0:24)
        INTEGER :: K_MMAXVAL_A (0:13)
        
        INTEGER, DIMENSION (0:1) :: K_PQ
        LOGICAL, DIMENSION (0:3, 0:17) :: L_M
        INTEGER, DIMENSION (0:3, 0:17) :: I_A
        INTEGER :: I_I
        INTEGER :: I_J
        INTEGER, DIMENSION (0:5) :: I_MAXVAL_A
        
        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_MM = (/66, 0, K_HQ, 0, 0, 6, 35, 1, 1, 4, 18, 1, 87, 6, 35, 2,
     * -2, 14, 100, 7, 5, 2, 4, 0, 1/)
        K_MMAXVAL_A = (/129, 0, 0, 0, 6, 1, 6, 0, 6, 1, 6, 6, 1, -1/)
        
        DO 10 I_I = 1, 6
          DO 20 I_J = 1, 35
            IF (K_PQ(0) == ((I_I-1)*2+1)/7 .AND. K_PQ(1) == MOD((-(I_J-1
     *)*2+87)/5,3)) THEN
              K_V0 = MOD((I_I-1)*2+1,7)/2
              K_V1 = MOD(-(I_J-1)*2+87,5)/2+(-(I_J-1)*2+87)/15*3
              L_M (K_V0,K_V1) = .FALSE.
            ENDIF
            
            IF (I_J.LT.13) THEN
              IF (K_PQ(0) == ((I_I-1)*2+1)/7 .AND. K_PQ(1) == MOD((-(I_J
     *-1)*2+87)/5,3)) THEN
                K_V0 = MOD((I_I-1)*2+1,7)/2
                K_V1 = MOD(-(I_J-1)*2+87,5)/2+(-(I_J-1)*2+87)/15*3
                I_A (K_V0,K_V1) = I_I
              ENDIF
              
            ELSE
              IF (K_PQ(0) == ((I_I-1)*2+1)/7 .AND. K_PQ(1) == MOD((-(I_J
     *-1)*2+87)/5,3)) THEN
                K_V0 = MOD((I_I-1)*2+1,7)/2
                K_V1 = MOD(-(I_J-1)*2+87,5)/2+(-(I_J-1)*2+87)/15*3
                I_A (K_V0,K_V1) = 1
              ENDIF
              
            ENDIF
            
   20     CONTINUE
   10   CONTINUE
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 0) THEN
          L_M (0,16) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 1) THEN
          L_M (0,12) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 1) THEN
          L_M (0,8) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 0 .AND. K_PQ(1) == 0) THEN
          L_M (2,5) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 1 .AND. K_PQ(1) == 2) THEN
          L_M (0,16) = .TRUE.
        ENDIF
        
        IF (K_PQ(0) == 1 .AND. K_PQ(1) == 2) THEN
          L_M (0,15) = .TRUE.
        ENDIF
        
        DO 30 I_J = 1, 35
          IF (K_PQ(0) == 1 .AND. K_PQ(1) == MOD((-(I_J-1)*2+87)/5,3)) TH
     *EN
            K_V0 = MOD(-(I_J-1)*2+87,5)/2+(-(I_J-1)*2+87)/15*3
            L_M (1,K_V0) = .TRUE.
          ENDIF
          
   30   CONTINUE
        K_T0 = K_PTR;   K_PTR = K_PTR + 4
        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) = (/65, 0, K_HQ, 0, 6, 1, 4, 1, 6, 2, 14,
     * 7, 2, 0/)
        
        CALL AD_MAXVAL_DIM_I (K_TMP(K_T0), K_TMP(K_M0), I_A, K_MM, 2, N_
     *TMP(0), (/0/), .FALSE.)
        
        K_T1 = K_PTR;   K_PTR = K_PTR + 6
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        CALL AD_REMAP_II (K_TMP(K_T1), K_MMAXVAL_A, K_TMP(K_T0), K_TMP(K
     *_M0))
        
        K_T1_0 = K_T1
        DO K_0 = 0, 5
          I_MAXVAL_A (K_0) = K_TMP (K_T1_0)
          K_T1_0 = K_T1_0 + (1)
        ENDDO
        
        K_PTR = K_PTR - 24
        IF (K_P0 == 0) THEN
          PRINT "(6i4)", I_MAXVAL_A
        ENDIF
        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
