#![allow(unused_imports)] #![allow(dead_code)] use std::collections::{HashMap, HashSet}; use std::io::{self, BufRead, BufReader, Read, Stdin}; use std::iter; use std::mem; use std::str::FromStr; use std::string::String; #[derive(Debug)] pub struct Scanner<R> { reader: BufReader<R>, } fn scanner() -> Scanner<Stdin> { Scanner::new(io::stdin()) } impl<R: io::Read> Scanner<R> { pub fn new(read: R) -> Scanner<R> { Scanner { reader: BufReader::new(read), } } pub fn next_str(&mut self) -> Option<String> { let mut buf = [0; 1]; let size = self.reader.read(&mut buf).unwrap(); if size == 0 { None } else { self.skip_whitespace(&mut buf)?; let mut v = vec![buf[0]]; loop { let size = self.reader.read(&mut buf).unwrap(); if size == 0 || buf[0].is_ascii_whitespace() { break; } v.push(buf[0]); } Some(String::from_utf8(v).unwrap()) } } pub fn next_line(&mut self) -> String { let mut line = String::new(); self.reader.read_line(&mut line).unwrap(); if let Some(s) = line.strip_suffix("\n") { s.to_string() } else { line } } pub fn vec<S: FromStr>(&mut self, size: i32) -> Vec<S> { let mut v: Vec<S> = vec![]; for _ in 0..size { let token = self.next_str().unwrap(); v.push(S::from_str(&token).ok().unwrap()); } v } pub fn next_as<S: FromStr>(&mut self) -> Option<S> { let s = self.next_str()?; S::from_str(&s).ok() } pub fn str(&mut self) -> String { self.next_str().unwrap() } pub fn i32(&mut self) -> i32 { self.next_as::<i32>().unwrap() } pub fn u32(&mut self) -> u32 { self.next_as::<u32>().unwrap() } pub fn i64(&mut self) -> i64 { self.next_as::<i64>().unwrap() } pub fn u64(&mut self) -> u64 { self.next_as::<u64>().unwrap() } pub fn usize(&mut self) -> usize { self.next_as::<usize>().unwrap() } pub fn f32(&mut self) -> f32 { self.next_as::<f32>().unwrap() } pub fn f64(&mut self) -> f64 { self.next_as::<f64>().unwrap() } fn skip_whitespace(&mut self, mut buf: &mut [u8]) -> Option<u8> { loop { if !buf[0].is_ascii_whitespace() { return Some(buf[0]); } let size = self.reader.read(&mut buf).unwrap(); if size == 0 { return None; } } } } fn yesno(b: bool) -> &'static str { if b { "Yes" } else { "No" } } // end of library fn main() { let mut sc = scanner(); let n = sc.i32(); let mut set = HashSet::new(); for _ in 0..n { let x = sc.i32(); let y = sc.i32(); set.insert(y - x); } let first = set.iter().next().unwrap(); if set.len() == 1 && *first > 0 { println!("{}", first); } else { println!("{}", -1); } }