fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map(String::toInt)
    val groupNumList = Array(n + 2, { 1 })
    val inputList = mutableListOf<Pair<Int,Int>>()
    for (i in 0 until m) {
        val (a, b) = readLine()!!.split(' ').map(String::toInt)
        inputList.add(Pair(a,b))
    }
    inputList.sortBy { it.first }
    for (i in 0 until m) {
        val (a, b) = inputList[i]
        if (groupNumList[a] == groupNumList[b]) {
            for (i in b..n) {
                groupNumList[i] = groupNumList[i] + 1
                if (groupNumList[i] == groupNumList[i + 1]) break
            }
        }
    }
    println(groupNumList[n] - 1)
}
