extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap};
use std::fmt::{Display, Formatter, Error};
use std::hash::{Hash, Hasher};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
trait Order {
    type Output;
    fn sorted(&self) -> Self::Output;
}
impl <T: Ord + Clone> Order for Vec<T> {
    type Output = Self;
    fn sorted(&self) -> Self::Output {
        let mut result = self.clone();
        result.sort();
        result
    }
}
trait SortedVec<T> {
    fn lower_bound(&self, target: &T) -> usize;
    fn upper_bound(&self, target: &T) -> usize;
}
impl <T: Ord> SortedVec<T> for Vec<T> {
    fn lower_bound(&self, target: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = (left + right) / 2;
            if self[mid] < *target {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        right
    }

    fn upper_bound(&self, target: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = (left + right) / 2;
            if self[mid] <= *target {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        right
    }
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
struct Point {
    x: i32, y: i32
}
impl Hash for Point {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        self.y.hash(state);
    }
}
#[derive(Copy, Clone)]
struct Edge {
    to: usize, length: i32
}
fn abs(value: i32) -> i32 {
    if value < 0{
        -value
    }else {
        value
    }
}
fn main() {
    let_all!(n: i32, m: i32, k: usize);
    let mut points = HashMap::new();
    let mut rocks = vec![Vec::new(); k + 1];
    let mut deque = VecDeque::new();
    let mut min_step = vec![std::i32::MAX; k + 1];
    for i in 0 .. k {
        let_all!(x: i32, y: i32);
        for dx in -2 .. 3 {
            for dy in -2 .. 3 {
                if dx != 0 || dy != 0 {
                    if let Some(&j) = points.get(&Point{x: x + dx, y: y + dy}) {
                        rocks[i].push(Edge{to: j, length: max(abs(dx), abs(dy)) - 1});
                        rocks[j].push(Edge{to: i, length: max(abs(dx), abs(dy)) - 1});
                    }
                }
            }
        }
        if x == 0 || y == m - 1 {
            min_step[i] = 0;
            deque.push_front(i);
        }else if x == 1 || y == m - 2 {
            min_step[i] = 1;
            deque.push_back(i);
        }
        if x == n - 1 || y == 0 {
            rocks[i].push(Edge{to: k, length: 0});
        }else if x == n - 2 || y == 1 {
            rocks[i].push(Edge{to: k, length: 1});
        }
        points.insert(Point{x: x, y: y}, i);
    }
    while let Some(current) = deque.pop_front() {
        if current == k {
            break
        }
        if min_step[current] != -1 {
            for &e in &rocks[current] {
                if min_step[e.to] > min_step[current] + e.length {
                    min_step[e.to] = min_step[current] + e.length;
                    if e.length == 0 {
                        deque.push_front(e.to);
                    }else {
                        deque.push_back(e.to);
                    }
                }
            }
            min_step[current] = -1;
        }
    }
    println!("{}", min(2, min_step[k]));
}


Measured memory_bytes: 31548