      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_MB (0:26)
        INTEGER :: K_MU (0:15)
        INTEGER :: K_MV (0:15)
        INTEGER :: K_MA (0:24)
        
        INTEGER, DIMENSION (0:1) :: K_PQ
        INTEGER, DIMENSION (0:4, 0:3) :: I_B
        INTEGER :: I_J
        INTEGER, DIMENSION (0:4) :: I_U
        INTEGER, DIMENSION (0:3) :: I_V
        INTEGER :: I_I
        INTEGER, DIMENSION (0:1) :: K_PP
        INTEGER, DIMENSION (0:2, 0:5) :: I_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)
        CALL AD_INIT_PROCS (K_HP, 2, (/2, 3/))
        CALL AD_PROC_COORD (K_HP, K_PP)
        
        K_MB = (/74, 0, K_HQ, 0, 0, 5, 5, 1, 1, 5, 4, 0, 6, 5, 5, 1, -1,
     * 5, 10, 5, 4, 1, 2, -1, 1, 0, 0/)
        K_MU = (/73, 0, K_HQ, 0, 5, 1, 5, 0, 5, 1, 5, 5, 1, -1, 0, 0/)
        K_MV = (/73, 0, K_HQ, 0, 5, 1, 4, 6, 5, -1, 10, 4, 2, 1, 0, 0/)
        K_MA = (/66, 0, K_HP, 0, 0, 6, 6, 1, 1, 3, 6, 0, 0, 6, 6, 1, 1, 
     *6, 6, 1, 6, 4, 1, 0, -1/)
        
        DO 10 I_I = 1, 5
          DO 20 I_J = 1, 5
            IF (K_PQ(0) == 0 .AND. K_PQ(1) == (-(I_J-1)+6)/4) THEN
              K_V0 = I_I-1
              K_V1 = MOD(-(I_J-1)+6,4)
              I_B (K_V0,K_V1) = I_J+(I_I-1)*10-1
            ENDIF
            
   20     CONTINUE
   10   CONTINUE
        DO 30 I_I = 1, 5
          IF (K_PQ(0) == 0) THEN
            K_V0 = MOD((I_I-1)*11,6)
          ENDIF
          
          IF (K_PQ(0) == 0) THEN
            K_V1 = I_I-1
            I_U (K_V1) = K_V0+1
          ENDIF
          
   30   CONTINUE
        DO 40 I_J = 1, 5
          IF (K_PQ(1) == (-(I_J-1)+6)/4 .AND. K_PQ(0) == 0) THEN
            K_V0 = MOD(I_J+2,6)
          ENDIF
          
          IF (K_PQ(0) == 0 .AND. K_PQ(1) == (-(I_J-1)+6)/4) THEN
            K_V1 = MOD(-(I_J-1)+6,4)
            I_V (K_V1) = K_V0+1
          ENDIF
          
   40   CONTINUE
        DO 50 I_I = 1, 6
          DO 60 I_J = 1, 6
            IF (K_PP(0) == MOD(I_I-1,2)) THEN
              K_V0 = (I_I-1)/2
              K_V1 = I_J-1
              I_A (K_V0,K_V1) = 0.0
            ENDIF
            
   60     CONTINUE
   50   CONTINUE
        K_T0 = K_PTR;   K_PTR = K_PTR + 4
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_T1 = 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_HP, 0, 5, 1, 3, 0, 5, 1, 5, 
     *3, 2, 0/)
        
        CALL AD_REMAP_II (K_TMP(K_T1), K_TMP(K_M0), I_U, K_MU)
        
        K_V0 = MIN(-K_PP(0)*3+4,2)
        K_T1_0 = K_T1
        K_T0_0 = K_T0
        DO K_0 = 0, K_V0
          K_TMP (K_T0_0) = K_TMP (K_T1_0)-1
          K_T1_0 = K_T1_0 + (1)
          K_T0_0 = K_T0_0 + (1)
        ENDDO
        
        K_T2 = K_PTR;   K_PTR = K_PTR + 6
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_T3 = K_PTR;   K_PTR = K_PTR + 6
        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) = (/257, 0, K_HP, 0, 5, 1, 5, 0, 5, 1, 5,
     * 5, 1, -1/)
        
        CALL AD_REMAP_II (K_TMP(K_T3), K_TMP(K_M1), I_V, K_MV)
        
        K_T3_0 = K_T3
        K_T2_0 = K_T2
        DO K_0 = 0, 4
          K_TMP (K_T2_0) = K_TMP (K_T3_0)-1
          K_T3_0 = K_T3_0 + (1)
          K_T2_0 = K_T2_0 + (1)
        ENDDO
        
        K_T4 = K_PTR;   K_PTR = K_PTR + 16
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_M2 = K_PTR;   K_PTR = K_PTR + 26
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M2 : K_M2+24) = (/66, 0, K_HP, 0, 0, 5, 5, 1, 1, 3, 5, 
     *0, 0, 5, 5, 1, 1, 5, 5, 3, 5, 2, 1, 0, -1/)
        
        CALL AD_REMAP_II (K_TMP(K_T4), K_TMP(K_M2), I_B, K_MB)
        
        K_M3 = K_PTR;   K_PTR = K_PTR + 26
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        K_TMP (K_M3 : K_M3+8) = (/66, 0, K_HP, 0, 0, -2, -2, 1, 1/)
        K_TMP (K_M3+9 : K_M3+24) = K_MA (9 : 24)
        
        CALL AD_SCATTER_II (K_TMP(K_T4), K_TMP(K_M2), I_A, K_TMP(K_M3), 
     *K_TMP(K_T0), K_TMP(K_M0), K_TMP(K_T2), K_TMP(K_M1))
        K_PTR = K_PTR - 26
        
        K_PTR = K_PTR - 90
        DO 70 I_I = 1, 6
          DO 80 I_J = 1, 6
            K_V0 = I_I-1
            
            K_V1 = I_J-1
            
            K_M0 = K_PTR;   K_PTR = K_PTR + 4
            IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
            K_TMP (K_M0 : K_M0+2) = (/256, 0, K_H0/)
            
            K_M1 = K_PTR;   K_PTR = K_PTR + 26
            IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
            K_TMP (K_M1 : K_M1+8) = (/66, 0, K_HP, I_I-1, I_J-1, -1, -1,
     * 1, 1/)
            K_TMP (K_M1+9 : K_M1+24) = K_MA (9 : 24)
            
            CALL AD_REMAP_II (K_V2, K_TMP(K_M0), I_A, K_TMP(K_M1))
            K_PTR = K_PTR - 26
            
            IF (K_P0 == 0) THEN
              PRINT "(2i3, i10)", K_V0,K_V1,K_V2
            ENDIF
            K_PTR = K_PTR - 4
   80     CONTINUE
   70   CONTINUE
        CALL AD_DELETE_PROCS (K_HQ)
        CALL AD_DELETE_PROCS (K_HP)
        
        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
