#![allow(non_snake_case)] use crate::{arraylist::List, scanner::Scanner}; fn main() { let mut scan = Scanner::new(); let n = scan.long(); let m = scan.int(); let mut lr = List::gen(m, |_| (scan.long() - 1, scan.long() - 1)); lr.sort_by_key(|t| t.1); let mut cnt = 0; let mut x = -1; for &(l, r) in &lr { if x < l { x = r; cnt += 1; } } println!("{}", n - cnt); } pub mod scanner { use std::io::{stdin, BufReader, Bytes, Read, Stdin}; use std::str::FromStr; pub struct Scanner { buf: Bytes>, } impl Scanner { pub fn new() -> Scanner { Scanner { buf: BufReader::new(stdin()).bytes(), } } #[inline] fn token>(&mut self) -> T { self.buf .by_ref() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect() } #[inline] pub fn read(&mut self) -> T { self.string().parse().ok().unwrap() } #[inline] pub fn string(&mut self) -> String { self.token() } #[inline] pub fn int(&mut self) -> isize { self.read() } #[inline] pub fn long(&mut self) -> i64 { self.read() } } } pub mod arraylist { use std::ops::*; use std::slice::Iter; use std::fmt::Formatter; use std::iter::FromIterator; #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct List { pub data: Vec, } impl List { #[inline] pub fn gen<'a, S>(n: isize, f: S) -> List where S: FnMut(isize) -> T + 'a, { (0..n).map(f).collect() } } macro_rules! impl_idx { ($($tpe:ty, $t:ident [$($output:tt)+], $slf:ident, $index:ident, $f:expr),*) => { $(impl<$t> Index<$tpe> for List<$t> { type Output = $($output)+; #[inline] fn index(&$slf, $index: $tpe) -> &Self::Output {$f} })* $(impl<$t> Index<$tpe> for lst<$t> { type Output = $($output)+; #[inline] fn index(&$slf, $index: $tpe) -> &Self::Output {$f} })* } } macro_rules! impl_idx_mut { ($($tpe:ty, $slf:ident, $index:ident, $f:expr),*) => { $(impl IndexMut<$tpe> for List { #[inline] fn index_mut(&mut $slf, $index: $tpe) -> &mut Self::Output {$f} })* $(impl IndexMut<$tpe> for lst { #[inline] fn index_mut(&mut $slf, $index: $tpe) -> &mut Self::Output {$f} })* }; } macro_rules! impl_idx_slice { ($($tpe:ty),*) => { impl_idx!($($tpe, T [lst], self, i, self.as_slice(i)),*); impl_idx_mut!($($tpe, self, i, self.as_slice_mut(i)),*); }; } impl_idx! { isize, T [T], self, i, self.at(i), char, T [T], self, i, self.at(i as isize - 'a' as isize) } impl_idx_slice! { Range, RangeTo, RangeFrom, RangeFull, RangeInclusive, RangeToInclusive } impl_idx_mut! { isize, self, i, self.at_mut(i), char, self, i, self.at_mut(i as isize - 'a' as isize) } impl FromIterator for List { #[inline] fn from_iter>(iter: U) -> Self { List { data: iter.into_iter().collect(), } } } impl IntoIterator for List { type Item = T; type IntoIter = std::vec::IntoIter; #[inline] fn into_iter(self) -> std::vec::IntoIter { self.data.into_iter() } } macro_rules! impl_traits { ($($tpe:tt),*) => { $( impl std::fmt::Display for $tpe { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", self.iter().map(|x| format!("{}", x)).collect::>().join(" ")) } } impl std::fmt::Debug for $tpe { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.data.fmt(f) } } impl<'a, T> IntoIterator for &'a $tpe { type Item = &'a T; type IntoIter = Iter<'a, T>; #[inline] fn into_iter(self) -> Iter<'a, T> { self.iter() } } )* }; } impl_traits!(List, lst); impl From> for List { #[inline] fn from(vec: Vec) -> Self { List { data: vec } } } impl From<&[T]> for List { #[inline] fn from(slice: &[T]) -> Self { slice.iter().cloned().collect() } } impl Deref for List { type Target = lst; #[inline] fn deref(&self) -> &lst { lst::new(&self.data) } } impl DerefMut for List { #[inline] fn deref_mut(&mut self) -> &mut lst { lst::new_mut(&mut self.data) } } #[macro_export] macro_rules! list { () => { $crate::arraylist::List::new() }; ($($v:expr),+ $(,)?) => { $crate::arraylist::List::from([$($v),+].to_vec()) }; ($v:expr; $a:expr) => { $crate::arraylist::List::init($v, $a)}; ($v:expr; $a:expr; $($rest:expr);+) => { $crate::arraylist::List::init(list!($v; $($rest);+), $a) }; } #[allow(non_camel_case_types)] #[derive(PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] pub struct lst { data: [T], } impl lst { #[inline] pub fn new(slice: &[T]) -> &Self { unsafe { &*(slice as *const [T] as *const Self) } } #[inline] pub fn new_mut(slice: &mut [T]) -> &mut Self { unsafe { &mut *(slice as *mut [T] as *mut Self) } } #[inline] fn at(&self, index: isize) -> &T { if cfg!(debug_assertions) { self.data.index(index as usize) } else { unsafe { self.data.get_unchecked(index as usize) } } } #[inline] fn at_mut(&mut self, index: isize) -> &mut T { if cfg!(debug_assertions) { self.data.index_mut(index as usize) } else { unsafe { self.data.get_unchecked_mut(index as usize) } } } #[inline] pub fn as_slice(&self, range: impl RangeBounds) -> &lst { if cfg!(debug_assertions) { lst::new(self.data.index(self.rgm(range))) } else { unsafe { lst::new(self.data.get_unchecked(self.rgm(range))) } } } #[inline] pub fn as_slice_mut(&mut self, range: impl RangeBounds) -> &mut lst { if cfg!(debug_assertions) { lst::new_mut(self.data.index_mut(self.rgm(range))) } else { let r = self.rgm(range); unsafe { lst::new_mut(self.data.get_unchecked_mut(r)) } } } #[inline] fn rgm(&self, r: impl RangeBounds) -> Range { (match r.start_bound() { Bound::Included(x) => *x as usize, Bound::Excluded(x) => *x as usize + 1, _ => 0, }) .max(0)..(match r.end_bound() { Bound::Included(x) => *x as usize + 1, Bound::Excluded(x) => *x as usize, _ => self.len(), }) .min(self.len()) } } impl lst {} impl Deref for lst { type Target = [T]; #[inline] fn deref(&self) -> &[T] { &self.data } } impl DerefMut for lst { #[inline] fn deref_mut(&mut self) -> &mut [T] { &mut self.data } } impl<'a, T> From<&'a [T]> for &'a lst { #[inline] fn from(slice: &'a [T]) -> Self { lst::new(slice) } } }