Problem:
Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

// ---------- begin Strongly Connected Components ----------
struct SCCforSAT2 {
    size: usize,
    edge: Vec<(u32, u32)>,
    id: Vec<usize>,
}

impl SCCforSAT2 {
    pub fn new(size: usize) -> Self {
        SCCforSAT2 {
            size: size,
            edge: vec![],
            id: Vec::with_capacity(size),
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        assert!(from < self.size && to < self.size);
        self.edge.push((from as u32, to as u32));
    }
    #[inline(never)]
    fn build_graph<'a>(&self, buf: &'a mut [u32], cnt: &[u32], inv: bool) -> Vec<&'a mut [u32]> {
        let size = self.size;
        let mut index = vec![0; self.size];
        for e in self.edge.iter() {
            let (f, t) = if inv { (e.1, e.0) } else { *e };
            buf[self.edge.len() - cnt[f as usize] as usize + index[f as usize]] = t;
            index[f as usize] += 1;
        }
        let mut buf = Some(buf);
        let mut ans = Vec::with_capacity(size);
        for i in 0..size {
            let len = cnt[i] - cnt[i + 1];
            let x = buf.take().unwrap();
            let (x, y) = x.split_at_mut(len as usize);
            ans.push(x);
            buf = Some(y);
        }
        ans
    }
    #[inline(never)]
    fn dfs1(&self, buf: &mut [u32], cnt: &[u32]) -> Vec<u32> {
        let size = self.size;
        let graph = self.build_graph(buf, cnt, false);
        let mut q = Vec::with_capacity(size);
        let mut visited = vec![false; size];
        let mut stack = vec![];
        for v in 0..size {
            if visited[v] {
                continue;
            }
            stack.clear();
            visited[v] = true;
            stack.push((v, graph[v].iter()));
            while let Some((v, mut it)) = stack.pop() {
                let mut finish = true;
                while let Some(&u) = it.next() {
                    let u = u as usize;
                    if !visited[u] {
                        visited[u] = true;
                        finish = false;
                        stack.push((v, it));
                        stack.push((u, graph[u].iter()));
                        break;
                    }
                }
                if finish {
                    q.push(v as u32);
                }
            }
        }
        q
    }
    #[inline(never)]
    fn dfs2(&mut self, buf: &mut [u32], cnt: &[u32], q: &[u32]) {
        let size = self.size;
        let inv_graph = self.build_graph(buf, cnt, true);
        self.id.clear();
        self.id.resize(size, size);
        let mut counter = 0;
        let mut stack = vec![];
        for &v in q.iter().rev() {
            let v = v as usize;
            if self.id[v] < size {
                continue;
            }
            self.id[v] = counter;
            stack.clear();
            stack.push(v);
            while let Some(v) = stack.pop() {
                self.id[v] = counter;
                for &u in inv_graph[v].iter() {
                    let u = u as usize;
                    if self.id[u] == size {
                        self.id[u] = counter;
                        stack.push(u);
                    }
                }
            }
            counter += 1;
        }
    }
    #[inline(never)]
    pub fn build(&mut self) {
        let size = self.size;
        let mut cnt = vec![0; size + 1];
        let mut inv_cnt = vec![0; size + 1];
        for e in self.edge.iter() {
            cnt[e.0 as usize] += 1;
            inv_cnt[e.1 as usize] += 1;
        }
        for i in (0..size).rev() {
            cnt[i] += cnt[i + 1];
            inv_cnt[i] += inv_cnt[i + 1];
        }
        let mut buf = vec![0; self.edge.len()];
        let buf = buf.as_mut_slice();
        let q = self.dfs1(buf, &cnt);
        self.dfs2(buf, &inv_cnt, &q);
    }
    pub fn get_array(&self) -> Vec<usize> {
        self.id.clone()
    }
}
// ---------- end Strongly Connected Components ----------
// ---------- begin 2-SAT ----------
struct SAT2 {
    size: usize,
    scc: SCCforSAT2,
}

impl SAT2 {
    fn new(size: usize) -> Self {
        SAT2 {
            size: size,
            scc: SCCforSAT2::new(2 * size),
        }
    }
    fn add(&mut self, a: usize, b: usize) {
        let size = self.size;
        let (x, ix) = if a >= size {
            (!a + size, !a)
        } else {
            (a, a + size)
        };
        let (y, iy) = if b >= size {
            (!b + size, !b)
        } else {
            (b, b + size)
        };
        assert!(x < 2 * size && y < 2 * size && ix < 2 * size && iy < 2 * size);
        self.scc.add_edge(ix, y);
        self.scc.add_edge(iy, x);
    }
    fn solve(&mut self) -> Option<Vec<bool>> {
        self.scc.build();
        let id = self.scc.get_array();
        let mut ans = vec![];
        for i in 0..self.size {
            if id[i] == id[i + self.size] {
                return None;
            } else if id[i] < id[i + self.size] {
                ans.push(false);
            } else {
                ans.push(true);
            }
        }
        Some(ans)
    }
}
// ---------- end 2-SAT ----------

use std::io::Read;

fn can(m: i32, p: &[(i32, i32)], z: &[(i32, usize)]) -> bool {
    let size = z.len().next_power_of_two();
    let seg: Vec<usize> = (0..(2 * size)).collect();
    let mut sat = SAT2::new(2 * size);
    for i in 1..size {
        sat.add(seg[i], !seg[2 * i]);
        sat.add(seg[i], !seg[2 * i + 1]);
    }
    for (i, &(x, y)) in p.iter().enumerate() {
        let a = z.binary_search(&(x, i + 1)).unwrap();
        let b = z.binary_search(&(y, i + 1)).unwrap();
        sat.add(seg[a + size], seg[b + size]);
        for &(x, a, q) in [(x, a, seg[a + size]), (y, b, seg[b + size])].iter() {
            let l = z.binary_search(&(x - (m - 1), 0)).unwrap_err();
            let r = z.binary_search(&(x + (m - 1), p.len() + 1)).unwrap_err();
            for &(mut l, mut r) in [(l, a), (a + 1, r)].iter() {
                l += size;
                r += size;
                while l < r {
                    if l & 1 == 1 {
                        sat.add(!q, !seg[l]);
                        l += 1;
                    }
                    if r & 1 == 1 {
                        r -= 1;
                        sat.add(!q, !seg[r]);
                    }
                    l >>= 1;
                    r >>= 1;
                }
            }
        }
    }
    sat.solve().is_some()
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let mut p = vec![];
    let mut z = vec![];
    for i in 0..n {
        let x: i32 = it.next().unwrap().parse().unwrap();
        let y: i32 = it.next().unwrap().parse().unwrap();
        p.push((x, y));
        z.push((x, i + 1));
        z.push((y, i + 1));
    }
    z.sort();
    z.dedup();
    let mut ok = 0;
    let mut ng = 1_000_000_000i32 / (n - 1) as i32 + 1;
    while ng - ok > 1 {
        let m = (ok + ng) / 2;
        if can(m, &p, &z) {
            ok = m;
        } else {
            ng = m;
        }
    }
    println!("{}", ok);
}

fn main() {
    run();
}