Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5. In one second, you can perform one of the two following operations:  Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;  Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b. According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration. The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka). It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

NoteIn the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

function ProblemSolver() {
    this.HAS_TEST_CASES = false ;
    this.INPUT_FILE_NAME = "sample.in" ;
    this.OUTPUT_FILE_NAME = "sample.out" ;
    this.DO_OUTPUT_TO_FILE = false ;
    this.CLEAR_ARRAY_PER_CASE = true ;

    this.solveCase = function() {
        var res , i , j , fl , cn , a , b , c , m , prev ;
        res = 0 ;
        for( i = 0 ; i < this.k ; i++ ) {
            m = this.arr[ i ].length ;
            for( j = 0 ; j < m ; j++ ) {
                a = this.arr[ i ][ j ] ;
                this.brr[ a ] = i ;
            }
        }
        prev = this.brr[ 1 ] ;
        for( i = 2 ; i <= this.n ; i++ ) {
            a = this.brr[ i ] ;
            if( this.vis[ a ] == 1 ) {
                res += 2 ;
            }
            else {
                if( prev != a ) {
                    this.vis[ a ] = 1 ;
                    this.vis[ prev ] = 1 ;
                    res++ ;
                }
            }
            prev = a ;
        }
        print( res ) ;
    } ;

    this.init = function() {
        this.lim1 = 100010 ;
        this.lim2 = 110 ;
        this.cc = 1 ;
        this.ind = 1 ;
        this.n = 0 ;
        this.cn = 0 ;
        this.declareAndFillArrays() ;
    } ;

    this.getInput = function( irObj ) {
        var hasMoreInput , i , j , m ;
        hasMoreInput = true ;
        try {
        	this.n = irObj.nextInt() ;
            this.k = irObj.nextInt() ;
            this.arr = [] ;
            for( i = 0 ; i < this.k ; i++ ) {
                m = irObj.nextInt() ;
                this.arr.push( [] ) ;
                for( j = 0 ; j < m ; j++ ) {
                    this.arr[ i ].push( irObj.nextInt() ) ;
                }
            }
        }
        catch( ex ) {
            hasMoreInput = false ;
        }
        return hasMoreInput ;
    } ;

    this.clearArraysPerCase = function() {
        var i ;
        this.brr = new Array() ;
        this.vis = new Array() ;
        this.adjList = new Array() ;
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.brr.push( 0 ) ;
            this.vis.push( 0 ) ;
            this.adjList.push( new Array() ) ;
        }
    } ;

    this.clearPerCase = function() {
        this.cn = 0 ;
        this.cc++ ;
        if( this.CLEAR_ARRAY_PER_CASE == true ) {
            this.clearArraysPerCase() ;
        }
    } ;

    this.declareAndFillArrays = function() {
        var i , j ;
        this.srr = new Array() ;
        this.vis = new Array() ;
        this.arr = new Array() ;
        this.brr = new Array() ;
        this.memo = new Array() ;
        this.done = new Array() ;
        this.adjList = new Array() ;
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.srr.push( '' ) ;
            this.vis.push( 0 ) ;
            this.arr.push( 0 ) ;
            this.brr.push( 0 ) ;
            this.adjList.push( new Array() ) ;
        }
        for( i = 0 ; i < this.lim2 ; i++ ) {
            this.memo.push( new Array() ) ;
            this.done.push( new Array() ) ;
            for( j = 0 ; j < this.lim2 ; j++ ) {
                this.memo[ i ].push( -1 ) ;
                this.done[ i ].push( 0 ) ;
            }
        }
    } ;

    this.init() ;
}

function InputReader() {
    var self = this ;
    this.allLines = new Array() ;
    this.currrentLineNumber = 0 ;
    this.currrentCharacterIndex = 0 ;
    this.callBackFunction = null ;
    this.parentContext = null ;

    this.readAllLines = function() {
        var singleLine ;
        while( true ) {
            try {
                singleLine = readline() ;
                if( singleLine == null ) {
                    break ;
                }
            }
            catch( ex ) {
                break ;
            }
            this.allLines.push( singleLine ) ;
        }
    } ;

    this.readAllLinesFromRhino = function() {
        var brObj , line ;
        importPackage( java.io ) ;
        importPackage( java.lang ) ;
        brObj = new BufferedReader( new InputStreamReader( System[ 'in' ] ) ) ;
        this.allLines = [] ;
        while( true ) {
            line = brObj.readLine() ;
            if( line == null ) {
                break;
            }
            this.allLines.push( line ) ;
        }
    } ;

    this.readSingleLinesFromNodeJsJudgeServer = function( chunk ) {
        self.chunkData += chunk ;
    } ;

    this.readEndFromNodeJsJudgeServer = function() {
        self.parseRawData( self.chunkData ) ;
        self.parentContext.runCases() ;
    } ;

    this.readAllLinesFromNodeJsJudgeServer = function( parentContext ) {
        this.parentContext = parentContext ;
        process.stdin.resume() ;
        process.stdin.setEncoding( 'utf8' ) ;
        this.chunkData = '' ;
        process.stdin.on( 'data' , this.readSingleLinesFromNodeJsJudgeServer ) ;
        process.stdin.on( 'end' , this.readEndFromNodeJsJudgeServer ) ;
    } ;

    this.parseRawData = function( data ) {
        var len , i , currentString ;
        len = data.length ;
        currentString = '' ;
        this.allLines = [] ;
        for( i = 0 ; i < len ; i++ ) {
            if( data[ i ] == '\r' ) {
            }
            else if( data[ i ] == '\n' ) {
                this.allLines.push( currentString ) ;
                currentString = '' ;
            }
            else {
                currentString += data[ i ] ;
            }
        }
        if( currentString != '' ) {
            this.allLines.push( currentString ) ;
        }
    } ;

    this.readAllLinesFromFile = function( inputFileName ) {
        var currentDirectory , fsObj , inputFilePath , rawData ;
        fsObj = require( 'fs' ) ;
        currentDirectory = __dirname ;
        if( currentDirectory.indexOf( 'D:' ) != -1 ) {
            //on windows
            inputFilePath = currentDirectory + '\\' + inputFileName ;
        }
        else {
            //on linux
            inputFilePath = currentDirectory + '/' + inputFileName ;
        }
        rawData = fsObj.readFileSync( inputFilePath , "utf8" ) ;
        this.parseRawData( rawData ) ;
    } ;

    this.next = function( flag ) {
        var len , i , startIdx , res , endIdx ;
        if( flag == 0 ) {
            if( this.currrentCharacterIndex != 0 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
            }
            res = '' ;
            len = this.allLines[ this.currrentLineNumber ].length ;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                res += '' + this.allLines[ this.currrentLineNumber ].charAt( i ) ;
            }
            this.currrentLineNumber++ ;
            this.currrentCharacterIndex = 0 ;
            return res ;
        }
        while( true ) {
            if( this.currrentLineNumber >= this.allLines.length ) {
                throw new Error( 'No more tokens available!' ) ;
            }
            startIdx = -1 ;
            len = this.allLines[ this.currrentLineNumber ].length ;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) != ' '.charCodeAt( 0 ) ) {
                    startIdx = i ;
                    break ;
                }
            }
            if( startIdx == -1 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
                continue ;
            }
            res = '' ;
            endIdx = len - 1 ;
            for( i = startIdx ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) == ' '.charCodeAt( 0 ) ) {
                    endIdx = i ;
                    break ;
                }
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) >= 48 && this.allLines[ this.currrentLineNumber ].charCodeAt( i ) <= 57 ) {
                    res += '' + ( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) - 48 ) ;
                }
                else {
                    res += '' + this.allLines[ this.currrentLineNumber ].charAt( i ) ;
                }
            }
            this.currrentCharacterIndex = endIdx ;
            if( endIdx == len - 1 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
            }
            return res ;
        }
    } ;

    this.nextInt = function() {
        return parseInt( this.next( 1 ) ) ;
    } ;

    this.nextDouble = function() {
        return parseFloat( this.next( 1 ) ) ;
    } ;

    this.nextString = function() {
        return this.next( 1 ) ;
    } ;

    this.nextLine = function() {
        return this.next( 0 ) ;
    } ;
}

function FileOutputHandler() {
    this.resultantStringArray = new Array() ;

    this.addNewLine = function( newString ) {
        this.resultantStringArray.push( newString ) ;
    } ;

    this.clearPercase = function() {
        this.resultantStringArray = new Array() ;
    } ;

    this.flushOutOutputToFile = function( outputFileName ) {
        var i , sz , res , currentDirectory , fsObj , outputFilePath ;
        res = '' ;
        sz = this.resultantStringArray.length ;
        for( i = 0 ; i < sz ; i++ ) {
            if( i != 0 ) {
                res += '\n' ;
            }
            res += this.resultantStringArray[ i ] ;
        }
        fsObj = require( 'fs' ) ;
        currentDirectory = __dirname ;
        if( currentDirectory.indexOf( 'D:' ) != -1 ) {
            //on windows
            outputFilePath = currentDirectory + '\\' + outputFileName ;
        }
        else {
            //on linux
            outputFilePath = currentDirectory + '/' + outputFileName ;
        }
        fsObj.writeFileSync( outputFilePath , res ) ;
        this.clearPercase() ;
    } ;
}

function CodeExecutioner() {
    this.irObj = new InputReader() ;
    this.psObj = new ProblemSolver() ;
    this.fohObj = new FileOutputHandler() ;

    this.runCases = function() {
		var testCases , hasMoreTestCases , i ;
        if( this.psObj.HAS_TEST_CASES == true ) {
            testCases = this.irObj.nextInt() ;
            for( i = 0 ; i < testCases ; i++ ) {
                this.psObj.clearPerCase() ;
                this.psObj.getInput( this.irObj ) ;
                this.psObj.solveCase() ;
            }
        }
        else {
            while( true ) {
                this.psObj.clearPerCase() ;
                hasMoreTestCases = this.psObj.getInput( this.irObj ) ;
                if( hasMoreTestCases == false ) {
                    break;
                }
                this.psObj.solveCase() ;
            }
        }
        if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
            this.fohObj.flushOutOutputToFile( this.psObj.OUTPUT_FILE_NAME ) ;
        }
    } ;

    this.detectEnvironmentType = function() {
        var environmentType = null ;
        try {
            if( importPackage != null ) {
                environmentType = 'rhino' ;
            }
        }
        catch( ex1 ) {
            try {
                //for nodejs local server check
                if( __dirname != null && ( __dirname.indexOf( 'D:') != -1 || __dirname.indexOf( '/media/') != -1 ) ) {
                    environmentType = 'local-node-js' ;
                }
                else {
                    environmentType = 'server-node-js' ;
                }
            }
            catch( ex2 ) {
                try {
                    if( readline == null || typeof( readline ) != 'function' ) {
                        environmentType = 'server-node-js' ;
                    }
                    else {
                        try {
                            if( Map != null ) {
                                environmentType = 'spider-monkey' ;
                            }
                            else {
                                environmentType = 'javascript-v8' ;
                            }
                        }
                        catch( ex3 ) {
                            environmentType = 'javascript-v8' ;
                        }
                    }
                }
                catch( ex3 ) {
                    environmentType = 'server-node-js' ;
                }
            }
        }
        return environmentType ;
    } ;

    this.configureStreamsAndReadInput = function() {
        var localContext , isNodeJsJudgeServer , environmentType ;
        isNodeJsJudgeServer = false ;
        environmentType = this.detectEnvironmentType() ;
        if( environmentType == 'local-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log ;
                }
            }
            catch( ex ) {
                print = console.log ;
            }
            if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
                localContext = this.fohObj ;
                print = function() {
                    localContext.addNewLine.apply( localContext , arguments ) ;
                }
            }
            this.irObj.readAllLinesFromFile( this.psObj.INPUT_FILE_NAME ) ;
            this.runCases() ;
        }
        else if( environmentType == 'server-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log ;
                }
            }
            catch( ex ) {
                print = console.log ;
            }
            this.irObj.readAllLinesFromNodeJsJudgeServer( this ) ;
        }
        else if( environmentType == 'javascript-v8' ) {
            this.irObj.readAllLines() ;
            this.runCases() ;
        }
        else if( environmentType == 'rhino' ) {
            this.irObj.readAllLinesFromRhino() ;
            this.runCases() ;
        }
        else if( environmentType == 'spider-monkey' ) {
            this.irObj.readAllLines() ;
            this.runCases() ;
        }
    } ;

    this.configureStreamsAndReadInput() ;
}

new CodeExecutioner() ;

function ProblemSolver() {
    this.HAS_TEST_CASES = false ;
    this.INPUT_FILE_NAME = "sample.in" ;
    this.OUTPUT_FILE_NAME = "sample.out" ;
    this.DO_OUTPUT_TO_FILE = false ;
    this.CLEAR_ARRAY_PER_CASE = false ;

    this.solveCase = function() {
        var res , i , j , fl , cn , a , b , c , m , prev ;
        res = 0 ;
        for( i = 0 ; i < this.k ; i++ ) {
            m = this.arr[ i ].length ;
            for( j = 0 ; j < m ; j++ ) {
                a = this.arr[ i ][ j ] ;
                this.brr[ a ] = i ;
            }
        }
        prev = this.brr[ 1 ] ;
        for( i = 2 ; i <= this.n ; i++ ) {
            a = this.brr[ i ] ;
            if( this.vis[ a ] == 1 ) {
                res += 2 ;
            }
            else {
                if( prev != a ) {
                    this.vis[ a ] = 1 ;
                    res++ ;
                }
            }
            prev = a ;
        }
        print( res ) ;
    } ;

    this.init = function() {
        this.lim1 = 100010 ;
        this.lim2 = 110 ;
        this.cc = 1 ;
        this.ind = 1 ;
        this.n = 0 ;
        this.cn = 0 ;
        this.declareAndFillArrays() ;
    } ;

    this.getInput = function( irObj ) {
        var hasMoreInput , i , j , m ;
        hasMoreInput = true ;
        try {
        	this.n = irObj.nextInt() ;
            this.k = irObj.nextInt() ;
            this.arr = [] ;
            for( i = 0 ; i < this.k ; i++ ) {
                m = irObj.nextInt() ;
                this.arr.push( [] ) ;
                for( j = 0 ; j < m ; j++ ) {
                    this.arr[ i ].push( irObj.nextInt() ) ;
                }
            }
        }
        catch( ex ) {
            hasMoreInput = false ;
        }
        return hasMoreInput ;
    } ;

    this.clearArraysPerCase = function() {
        var i ;
        this.arr = new Array() ;
        this.brr = new Array() ;
        this.vis = new Array() ;
        this.adjList = new Array() ;
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.arr.push( 0 ) ;
            this.brr.push( 0 ) ;
            this.vis.push( 0 ) ;
            this.adjList.push( new Array() ) ;
        }
    } ;

    this.clearPerCase = function() {
        this.cn = 0 ;
        this.cc++ ;
        if( this.CLEAR_ARRAY_PER_CASE == true ) {
            this.clearArraysPerCase() ;
        }
    } ;

    this.declareAndFillArrays = function() {
        var i , j ;
        this.srr = new Array() ;
        this.vis = new Array() ;
        this.arr = new Array() ;
        this.brr = new Array() ;
        this.memo = new Array() ;
        this.done = new Array() ;
        this.adjList = new Array() ;
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.srr.push( '' ) ;
            this.vis.push( 0 ) ;
            this.arr.push( 0 ) ;
            this.brr.push( 0 ) ;
            this.adjList.push( new Array() ) ;
        }
        for( i = 0 ; i < this.lim2 ; i++ ) {
            this.memo.push( new Array() ) ;
            this.done.push( new Array() ) ;
            for( j = 0 ; j < this.lim2 ; j++ ) {
                this.memo[ i ].push( -1 ) ;
                this.done[ i ].push( 0 ) ;
            }
        }
    } ;

    this.init() ;
}

function InputReader() {
    var self = this ;
    this.allLines = new Array() ;
    this.currrentLineNumber = 0 ;
    this.currrentCharacterIndex = 0 ;
    this.callBackFunction = null ;
    this.parentContext = null ;

    this.readAllLines = function() {
        var singleLine ;
        while( true ) {
            try {
                singleLine = readline() ;
                if( singleLine == null ) {
                    break ;
                }
            }
            catch( ex ) {
                break ;
            }
            this.allLines.push( singleLine ) ;
        }
    } ;

    this.readAllLinesFromRhino = function() {
        var brObj , line ;
        importPackage( java.io ) ;
        importPackage( java.lang ) ;
        brObj = new BufferedReader( new InputStreamReader( System[ 'in' ] ) ) ;
        this.allLines = [] ;
        while( true ) {
            line = brObj.readLine() ;
            if( line == null ) {
                break;
            }
            this.allLines.push( line ) ;
        }
    } ;

    this.readSingleLinesFromNodeJsJudgeServer = function( chunk ) {
        self.chunkData += chunk ;
    } ;

    this.readEndFromNodeJsJudgeServer = function() {
        self.parseRawData( self.chunkData ) ;
        self.parentContext.runCases() ;
    } ;

    this.readAllLinesFromNodeJsJudgeServer = function( parentContext ) {
        this.parentContext = parentContext ;
        process.stdin.resume() ;
        process.stdin.setEncoding( 'utf8' ) ;
        this.chunkData = '' ;
        process.stdin.on( 'data' , this.readSingleLinesFromNodeJsJudgeServer ) ;
        process.stdin.on( 'end' , this.readEndFromNodeJsJudgeServer ) ;
    } ;

    this.parseRawData = function( data ) {
        var len , i , currentString ;
        len = data.length ;
        currentString = '' ;
        this.allLines = [] ;
        for( i = 0 ; i < len ; i++ ) {
            if( data[ i ] == '\r' ) {
            }
            else if( data[ i ] == '\n' ) {
                this.allLines.push( currentString ) ;
                currentString = '' ;
            }
            else {
                currentString += data[ i ] ;
            }
        }
        if( currentString != '' ) {
            this.allLines.push( currentString ) ;
        }
    } ;

    this.readAllLinesFromFile = function( inputFileName ) {
        var currentDirectory , fsObj , inputFilePath , rawData ;
        fsObj = require( 'fs' ) ;
        currentDirectory = __dirname ;
        if( currentDirectory.indexOf( 'D:' ) != -1 ) {
            //on windows
            inputFilePath = currentDirectory + '\\' + inputFileName ;
        }
        else {
            //on linux
            inputFilePath = currentDirectory + '/' + inputFileName ;
        }
        rawData = fsObj.readFileSync( inputFilePath , "utf8" ) ;
        this.parseRawData( rawData ) ;
    } ;

    this.next = function( flag ) {
        var len , i , startIdx , res , endIdx ;
        if( flag == 0 ) {
            if( this.currrentCharacterIndex != 0 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
            }
            res = '' ;
            len = this.allLines[ this.currrentLineNumber ].length ;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                res += '' + this.allLines[ this.currrentLineNumber ].charAt( i ) ;
            }
            this.currrentLineNumber++ ;
            this.currrentCharacterIndex = 0 ;
            return res ;
        }
        while( true ) {
            if( this.currrentLineNumber >= this.allLines.length ) {
                throw new Error( 'No more tokens available!' ) ;
            }
            startIdx = -1 ;
            len = this.allLines[ this.currrentLineNumber ].length ;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) != ' '.charCodeAt( 0 ) ) {
                    startIdx = i ;
                    break ;
                }
            }
            if( startIdx == -1 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
                continue ;
            }
            res = '' ;
            endIdx = len - 1 ;
            for( i = startIdx ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) == ' '.charCodeAt( 0 ) ) {
                    endIdx = i ;
                    break ;
                }
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) >= 48 && this.allLines[ this.currrentLineNumber ].charCodeAt( i ) <= 57 ) {
                    res += '' + ( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) - 48 ) ;
                }
                else {
                    res += '' + this.allLines[ this.currrentLineNumber ].charAt( i ) ;
                }
            }
            this.currrentCharacterIndex = endIdx ;
            if( endIdx == len - 1 ) {
                this.currrentLineNumber++ ;
                this.currrentCharacterIndex = 0 ;
            }
            return res ;
        }
    } ;

    this.nextInt = function() {
        return parseInt( this.next( 1 ) ) ;
    } ;

    this.nextDouble = function() {
        return parseFloat( this.next( 1 ) ) ;
    } ;

    this.nextString = function() {
        return this.next( 1 ) ;
    } ;

    this.nextLine = function() {
        return this.next( 0 ) ;
    } ;
}

function FileOutputHandler() {
    this.resultantStringArray = new Array() ;

    this.addNewLine = function( newString ) {
        this.resultantStringArray.push( newString ) ;
    } ;

    this.clearPercase = function() {
        this.resultantStringArray = new Array() ;
    } ;

    this.flushOutOutputToFile = function( outputFileName ) {
        var i , sz , res , currentDirectory , fsObj , outputFilePath ;
        res = '' ;
        sz = this.resultantStringArray.length ;
        for( i = 0 ; i < sz ; i++ ) {
            if( i != 0 ) {
                res += '\n' ;
            }
            res += this.resultantStringArray[ i ] ;
        }
        fsObj = require( 'fs' ) ;
        currentDirectory = __dirname ;
        if( currentDirectory.indexOf( 'D:' ) != -1 ) {
            //on windows
            outputFilePath = currentDirectory + '\\' + outputFileName ;
        }
        else {
            //on linux
            outputFilePath = currentDirectory + '/' + outputFileName ;
        }
        fsObj.writeFileSync( outputFilePath , res ) ;
        this.clearPercase() ;
    } ;
}

function CodeExecutioner() {
    this.irObj = new InputReader() ;
    this.psObj = new ProblemSolver() ;
    this.fohObj = new FileOutputHandler() ;

    this.runCases = function() {
		var testCases , hasMoreTestCases , i ;
        if( this.psObj.HAS_TEST_CASES == true ) {
            testCases = this.irObj.nextInt() ;
            for( i = 0 ; i < testCases ; i++ ) {
                this.psObj.clearPerCase() ;
                this.psObj.getInput( this.irObj ) ;
                this.psObj.solveCase() ;
            }
        }
        else {
            while( true ) {
                this.psObj.clearPerCase() ;
                hasMoreTestCases = this.psObj.getInput( this.irObj ) ;
                if( hasMoreTestCases == false ) {
                    break;
                }
                this.psObj.solveCase() ;
            }
        }
        if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
            this.fohObj.flushOutOutputToFile( this.psObj.OUTPUT_FILE_NAME ) ;
        }
    } ;

    this.detectEnvironmentType = function() {
        var environmentType = null ;
        try {
            if( importPackage != null ) {
                environmentType = 'rhino' ;
            }
        }
        catch( ex1 ) {
            try {
                //for nodejs local server check
                if( __dirname != null && ( __dirname.indexOf( 'D:') != -1 || __dirname.indexOf( '/media/') != -1 ) ) {
                    environmentType = 'local-node-js' ;
                }
                else {
                    environmentType = 'server-node-js' ;
                }
            }
            catch( ex2 ) {
                try {
                    if( readline == null || typeof( readline ) != 'function' ) {
                        environmentType = 'server-node-js' ;
                    }
                    else {
                        try {
                            if( Map != null ) {
                                environmentType = 'spider-monkey' ;
                            }
                            else {
                                environmentType = 'javascript-v8' ;
                            }
                        }
                        catch( ex3 ) {
                            environmentType = 'javascript-v8' ;
                        }
                    }
                }
                catch( ex3 ) {
                    environmentType = 'server-node-js' ;
                }
            }
        }
        return environmentType ;
    } ;

    this.configureStreamsAndReadInput = function() {
        var localContext , isNodeJsJudgeServer , environmentType ;
        isNodeJsJudgeServer = false ;
        environmentType = this.detectEnvironmentType() ;
        if( environmentType == 'local-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log ;
                }
            }
            catch( ex ) {
                print = console.log ;
            }
            if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
                localContext = this.fohObj ;
                print = function() {
                    localContext.addNewLine.apply( localContext , arguments ) ;
                }
            }
            this.irObj.readAllLinesFromFile( this.psObj.INPUT_FILE_NAME ) ;
            this.runCases() ;
        }
        else if( environmentType == 'server-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log ;
                }
            }
            catch( ex ) {
                print = console.log ;
            }
            this.irObj.readAllLinesFromNodeJsJudgeServer( this ) ;
        }
        else if( environmentType == 'javascript-v8' ) {
            this.irObj.readAllLines() ;
            this.runCases() ;
        }
        else if( environmentType == 'rhino' ) {
            this.irObj.readAllLinesFromRhino() ;
            this.runCases() ;
        }
        else if( environmentType == 'spider-monkey' ) {
            this.irObj.readAllLines() ;
            this.runCases() ;
        }
    } ;

    this.configureStreamsAndReadInput() ;
}

new CodeExecutioner() ;


Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night.In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the two hills there was no hill higher than any of the two. As for any two hills there are two different circle arcs connecting them, the signal was seen if the above mentioned condition was satisfied on at least one of the arcs. For example, for any two neighbouring watchmen it is true that the signal of one will be seen by the other.An important characteristics of this watch system was the amount of pairs of watchmen able to see each other's signals. You are to find this amount by the given heights of the hills.

The first line of the input data contains an integer number n (3 ≤ n ≤ 106), n — the amount of hills around the capital. The second line contains n numbers — heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109.

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _f = a[0] == 30332142;

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	//print( count[max], Q.length)
	if( count[max]==1 && Q.length==1 ){
		ret += (map[t]-1) * (map[t]-2) / 2;
		map[t] = 0;
	}else{
		ret += map[t] * (map[t]-1) / 2;
		map[t] = 0;
	}
}

var last = Q.top();

if(last == max){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var max = 0;
var maxc = 0;
for(var i=0; i<n; i++){
	if( a[i] > max ){
		max = a[i];
		maxc = 0;
	}else if( a[i] == max ){
		maxc++;
	}

	ret++;
	if( a[i] >= a[i+1] ){
		for(var j=i+2; j<i+n; j++){
			if( a[j-1] <= a[j]){
				ret++;
				if(a[i] < a[j]){
					break;
				}
			}else{
				break;
			}
		}
	}
}
//ret -= maxc==1?0:maxc;
ret -= Math.pow(2, maxc)-1;
print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ Q.top() ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();

if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var max = 0;
var maxc = 0;
for(var i=0; i<n; i++){
	if( a[i] > max ){
		max = a[i];
		maxc = 0;
	}else if( a[i] == max ){
		maxc++;
	}

	ret++;
	if( a[i] >= a[i+1] ){
		for(var j=i+2; j<i+n-1; j++){
			if( a[j-1] <= a[j]){
				ret++;
				if(a[i] < a[j]){
					break;
				}
			}else{
				break;
			}
		}
	}
}

print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var c =[];
for(var i=0; i<n; i++){
	c[i] = i;
}
for(var i=0; i<n; i++){
	//ret++;
	if( a[i] >= a[i+1] ){
		var max = a[i+1];
		var to = c[i]+n;

		for(var j=i+1; j < to; j++){
			if( a[j] >= max){
				ret++;
				max = a[j];
				if( i < c[j] ){
					c[j]=i;
				}
				if(max > a[i]){
					break;
				}
			}
		}
	}else{
		ret++;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _f = a[0] == 30332142;

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			if(_f && map[t]!=1){
				print(t);
				print(map[t]);
			}
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();

if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();
if(a[0]==30332142){
	print(last);
	print(map[last]);
	print(map);
}

if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){

	var top = Q.top();
	if( a[i] ==top ){
		map[ Q.top() ] ++;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
	}else{//a[i]<top
		if( count[ a[i] ] > 1 ){
			map[ a[i] ] = 1;
			Q.push( a[i] );
		}
	}
}

var last = Q.top();
if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

_f = a[0]==30332142;

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();
if(_f){
	print(last);
	print(map[last]);
	print(map);
}

if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){

	var top = Q.top();
	if( a[i] ==top ){
		map[ Q.top() ] ++;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
	}else{//a[i]<top
		if( count[ a[i] ] > 1 ){
			map[ a[i] ] = 1;
			Q.push( a[i] );
		}
	}

}
print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var max = 0;
var maxc = 1;
for(var i=0; i<n; i++){
	if( a[i] > max ){
		max = a[i];
		maxc = 1;
	}else if( a[i] == max ){
		maxc++;
	}

	ret++;
	if( a[i] >= a[i+1] ){
		for(var j=i+2; j<i+n; j++){
			if( a[j-1] <= a[j]){
				ret++;
				if(a[i] < a[j]){
					break;
				}
			}else{
				break;
			}
		}
	}
}
ret -= maxc==1?0:maxc;
print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){

	var top = Q.top();
	if( a[i] ==top ){
		map[ Q.top() ] ++;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
	}else{//a[i]<top
		if( count[ a[i] ] > 1 ){
			map[ a[i] ] = 1;
			Q.push( a[i] );
		}
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();
if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _f = a[0] == 30332142;

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	if(_f){
		print(t);
		print(map[t]);
	}
	ret += map[t] * (map[t]-1) / 2;
	map[t] = 0;
}

var last = Q.top();

if(last < Infinity){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var max = 0;
var maxc = 0;
for(var i=0; i<n; i++){
	if( a[i] > max ){
		max = a[i];
		maxc = 0;
	}else if( a[i] == max ){
		maxc++;
	}

	ret++;
	if( a[i] >= a[i+1] ){
		for(var j=i+2; j<i+n; j++){
			if( a[j-1] <= a[j]){
				ret++;
				if(a[i] < a[j]){
					break;
				}
			}else{
				break;
			}
		}
	}
}

print(ret-maxc);

var n = parseInt( readline() );
var count = {};
var maxIndex = 0;
var max = 0;

var a = readline().split(' ').map(function(v, i){
	if( !count[v] ){
		count[v] = 1;
	}else{
		count[v]++;
	}
	v = parseInt(v);
	if(v>max){
		max = v;
		maxIndex = i;
	}
	return v;
});

var _f = a[0] == 30332142;

var _a = [];
for(var i=maxIndex; i<n; i++){
	_a.push( a[i] );
}
for(var i=0; i<maxIndex; i++){
	_a.push( a[i] );
}
a = _a;

var Q = [];
Q.top = function(){
	return Q[Q.length-1];
}
Q.push( Infinity );

var map = {};

var ret = 2*n-3;

for(var i=0; i<n; i++){
	//print(map)
	var top = Q.top();
	if( a[i] == top ){
		map[ top ] ++;
		continue;
	}else if(a[i] > top){
		while( a[i]>Q.top() ){
			var t = Q.pop();
			ret += map[t] * (map[t]-1) / 2;
			map[t] = 0;
		}
		if(a[i]==Q.top()){
			map[ a[i] ] ++;
			continue;
		}
	}

	if( count[ a[i] ] > 1 ){
		map[ a[i] ] = 1;
		Q.push( a[i] );
	}
}

while( max>Q.top() ){
	var t = Q.pop();
	if( !(count[max]==1 && Q.length==1) ){
		ret += map[t] * (map[t]-1) / 2;
		map[t] = 0;
	}
}

var last = Q.top();

if(last == max){
	if(map[last]>=4){
		ret += (map[last]-2) * (map[last]-3) / 2;
	}
}

print(ret);

var n = parseInt( readline() );
var a = readline().split(' ').map(function(v){ return parseInt(v); });
a = a.concat(a);

var ret = 0;
var c =[];
for(var i=0; i<n; i++){
	c[i] = i;
}
for(var i=0; i<n; i++){
	ret++;
	if( a[i] >= a[i+1] ){
		var max = a[i+1];
		var to = c[i]+n-1;

		for(var j=i+2; j < to; j++){
			if( a[j] >= max){
				ret++;
				max = a[j];
				if( i < c[j] ){
					c[j]=i;
				}
				if(max > a[i]){
					break;
				}
			}
		}
	}
}

print(ret);