PROGRAM Main
    USE OMP_LIB
    IMPLICIT NONE
    INTEGER :: N, err, var, i

!$OMP PARALLEL DEFAULT(NONE), SHARED(N), PRIVATE(err,var,i)
    DO WHILE (.True.)
!$OMP BARRIER   ! The barrier is essential
!$OMP MASTER
        PRINT *, "Please type a positive integer"
        READ (*,*,IOSTAT=err) N
        IF (err < 0) STOP    ! This is allowed
        IF (err /= 0 .OR. N < 1) THEN
            PRINT *, "Invalid integer"
            STOP 1
        END IF
!$OMP END MASTER
!$OMP BARRIER   ! The barrier is essential
        var = N
        DO i = 1,omp_get_thread_num()+5000*5000
            var = MOD(5*var+1,1024)
        END DO
!$OMP CRITICAL
        PRINT *, "result =", var
!$OMP END CRITICAL
    END DO
!$OMP END PARALLEL
END PROGRAM Main
