use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
struct UnionFind {
    parent: Vec<i32>,
}

#[allow(dead_code)]
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            parent: vec![-1; n],
        }
    }
    fn root(&mut self, x: usize) -> usize {
        let p = self.parent[x];
        return if p < 0 {
            x
        } else {
            self.parent[x] = self.root(p as usize) as i32;
            self.parent[x] as usize
        };
    }
    fn union_set(&mut self, x: usize, y: usize) -> bool {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return false;
        }
        if self.parent[y] < self.parent[x] {
            std::mem::swap(&mut x, &mut y);
        }
        self.parent[x] += self.parent[y];
        self.parent[y] = x as i32;
        true
    }
    fn find_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn size(&mut self, x: usize) -> usize {
        let r = self.root(x);
        -self.parent[r] as usize
    }
}

#[test]
fn union_find() {
    let mut ufind = UnionFind::new(10);
    assert!(ufind.root(0) == 0);
    assert!(ufind.size(3) == 1);
    assert!(ufind.union_set(1, 2));
    assert!(!ufind.union_set(1, 2));
    ufind.union_set(2, 3);
    let r = ufind.root(3);
    assert!(r == 1 || r == 2 || r == 3);
    assert!(ufind.size(3) == 3);
}

fn main() {
    let n = read::<usize>();
    let m = read::<usize>();
    let mut aseq = vec![0i64; n];
    let mut bseq = vec![0i64; n];
    let mut cseq = vec![0i64; n];
    for i in 0..n {
        aseq[i] = read::<i64>();
        bseq[i] = read::<i64>();
        cseq[i] = std::cmp::max(0, aseq[i] - bseq[i]);
    }
    let mut edges = vec![(0, 0, 0); m];
    for i in 0..m {
        let u = read::<usize>() - 1;
        let v = read::<usize>() - 1;
        edges[i] = (std::cmp::max(cseq[u], cseq[v]), u, v);
    }
    edges.sort();
    let mut ufind = UnionFind::new(n);
    let mut dp = vec![0; n];
    let mut bsums = vec![0; n];
    let mut cmaxs = vec![0; n];
    let mut ans = 0;
    for i in 0..n {
        dp[i] = bseq[i] + cseq[i];
        bsums[i] = bseq[i];
        cmaxs[i] = cseq[i];
        ans = std::cmp::max(ans, dp[i]);
    }
    for i in 0..m {
        let (_c, mut u, mut v) = edges[i];
        u = ufind.root(u);
        v = ufind.root(v);
        if u == v {
            continue;
        }
        let ndp1 = std::cmp::max(dp[u], cmaxs[v]) + bsums[v];
        let ndp2 = std::cmp::max(dp[v], cmaxs[u]) + bsums[u];
        ufind.union_set(u, v);
        let r = ufind.root(u);
        dp[r] = *[ndp1, ndp2].iter().min().unwrap();
        cmaxs[r] = std::cmp::max(cmaxs[u], cmaxs[v]);
        bsums[r] = bsums[u] + bsums[v];
        ans = std::cmp::max(ans, dp[r]);
    }
    println!("{}", ans);
}


Measured memory_bytes: 14588