Problem:
Score : 1000 points

Problem Statement

For an n \times n grid, let (r, c) denote the square at the (r+1)-th row from the top and the (c+1)-th column from the left.
A good coloring of this grid using K colors is a coloring that satisfies the following:

Each square is painted in one of the K colors.

Each of the K colors is used for some squares.

Let us number the K colors 1, 2, ..., K. For any colors i and j (1 \leq i \leq K, 1 \leq j \leq K), every square in Color i has the same number of adjacent squares in Color j. Here, the squares adjacent to square (r, c) are ((r-1)\; mod\; n, c), ((r+1)\; mod\; n, c), (r, (c-1)\; mod\; n) and (r, (c+1)\; mod\; n) (if the same square appears multiple times among these four, the square is counted that number of times).

Given K, choose n between 1 and 500 (inclusive) freely and construct a good coloring of an n \times n grid using K colors.
It can be proved that this is always possible under the constraints of this problem,

Constraints

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K

Output

Output should be in the following format:

n
c_{0,0} c_{0,1} ... c_{0,n-1}
c_{1,0} c_{1,1} ... c_{1,n-1}
:
c_{n-1,0} c_{n-1,1} ... c_{n-1,n-1}

n should represent the size of the grid, and 1 \leq n \leq 500 must hold.
c_{r,c} should be an integer such that 1 \leq c_{r,c} \leq K and represent the color for the square (r, c).

Sample Input 1

2

Sample Output 1

3
1 1 1
1 1 1
2 2 2

Every square in Color 1 has three adjacent squares in Color 1 and one adjacent square in Color 2.

Every square in Color 2 has two adjacent squares in Color 1 and two adjacent squares in Color 2.

Output such as the following will be judged incorrect:

2
1 2
2 2

3
1 1 1
1 1 1
1 1 1

Sample Input 2

9

Sample Output 2

3
1 2 3
4 5 6
7 8 9

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp::{min,max};
#[allow(unused_imports)]
use std::collections::{BTreeMap,BTreeSet};
#[allow(unused_imports)]
use std::ops::*;
#[allow(unused_imports)]
use std::collections::BinaryHeap;

#[allow(unused_macros)]
macro_rules! ite {
    ($c:expr, $t:expr, $f:expr) => {{
        if $c { $t } else { $f }
    }};
}

// ref: tanakh <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
// diff: using Parser
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut parser = Parser::from_str($s);
        input_inner!{parser, $($r)*}
    };
    (parser = $parser:ident, $($r:tt)*) => {
        input_inner!{$parser, $($r)*}
    };
    (new_stdin_parser = $parser:ident, $($r:tt)*) => {
        let stdin = std::io::stdin();
        let reader = std::io::BufReader::new(stdin.lock());
        let mut $parser = Parser::new(reader);
        input_inner!{$parser, $($r)*}
    };
    ($($r:tt)*) => {
        input!{new_stdin_parser = parser, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($parser:ident) => {};
    ($parser:ident, ) => {};
    ($parser:ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($parser, $t);
        input_inner!{$parser $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($parser:ident, ( $($t:tt),* )) => {
        ( $(read_value!($parser, $t)),* )
    };
    ($parser:ident, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($parser, $t)).collect::<Vec<_>>()
    };
    ($parser:ident, chars) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()
    };
    ($parser:ident, usize1) => {
        read_value!($parser, usize) - 1
    };
    ($parser:ident, $t:ty) => {
        $parser.next::<$t>().expect("Parse error")
    };
}

/*
fn dfs(xs: &Vec<i64>, length: i64, memo: &mut Vec<Vec<Vec<i64>>>, l: usize, r: usize, prev_is_right: bool) -> i64 {
    if memo[l][r][ite!(prev_is_right,1,0)] >= 0 {
        return memo[l][r][ite!(prev_is_right,1,0)];
    }
    let prev = if prev_is_right {
        r - 1
    } else {
        l + 1
    };
    if l == r {
        let t = (xs[prev] - xs[l]).abs();
        return max(t, length - t);
    }
    // clockwise
    let t1 = (xs[r] - xs[prev] + 3 * length) % length;
    let res1 = dfs(xs, length, memo, l, r+1, true) + t1;
    // counter clockwise
    let t2 = (xs[prev] - xs[l] + 3 * length) % length;
    let res2 = dfs(xs, length, memo, l-1, r, false) + t2;
    let res = max(res1, res2);
    memo[l][r][ite!(prev_is_right,1,0)] = res;
    return res;
}

*/

fn main() {
    input! {
        n: usize,
    }
    if n == 1 {
        println!("1");
        println!("1");
        return;
    }
    if n == 2 {
        println!("2");
        println!("1 2");
        println!("1 2");
        return;
    }
    if n == 3 {
        println!("3");
        println!("1 2 3");
        println!("1 2 3");
        println!("1 2 3");
        return;
    }
    match n % 4 {
        0 => {
            let m = n / 2;
            println!("{}", m);
            for i in 0..n/2/2 {
                let mut xs = vec![];
                for k in 0..m {
                    xs.push((2*i+k)%m+1);
                }
                write_vec(&xs);
                let mut xs = vec![];
                for k in 0..m {
                    xs.push((2*i+k)%m+m+1);
                }
                write_vec(&xs);
            }
        }
        3 => {
            let m = (n+1) / 2;
            println!("{}", m);
            for i in 0..(n+1)/2/2 {
                let mut xs = vec![];
                for k in 0..m {
                    xs.push((2*i+k)%m+1);
                }
                write_vec(&xs);
                let mut xs = vec![];
                for k in 0..m {
                    let t = (2*i+k)%m+m+1;
                    if t == 2*m {
                        xs.push(t-m-1);
                    }
                    else {
                        xs.push(t);
                    }
                }
                write_vec(&xs);
            }
        }
        2 => {
            let m = (n+2) / 2;
            println!("{}", m);
            for i in 0..(n+2)/2/2 {
                let mut xs = vec![];
                for k in 0..m {
                    xs.push((2*i+k)%m+1);
                }
                write_vec(&xs);
                let mut xs = vec![];
                for k in 0..m {
                    let t = (2*i+k)%m+m+1;
                    if t == 2*m {
                        xs.push(t-m-1);
                    }
                    else if t == 2*m-1 {
                        xs.push(t-m-1);
                    }
                    else {
                        xs.push(t);
                    }
                }
                write_vec(&xs);
            }
        }
        1 => {
            let m = (n+3) / 2;
            println!("{}", m);
            for i in 0..(n+3)/2/2 {
                let mut xs = vec![];
                for k in 0..m {
                    xs.push((2*i+k)%m+1);
                }
                write_vec(&xs);
                let mut xs = vec![];
                for k in 0..m {
                    let t = (2*i+k)%m+m+1;
                    if t == 2*m {
                        xs.push(t-m-1);
                    }
                    else if t == 2*m-1 {
                        xs.push(t-m-1);
                    }
                    else if t == 2*m-2 {
                        xs.push(t-m-1);
                    }
                    else {
                        xs.push(t);
                    }
                }
                write_vec(&xs);
            }
        }
        _ => {
            panic!();
        }
    }
}

use std::io::BufRead;
use std::io;
use std::str;

// ref: tatsuya6502 <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e>
// ref: wariuni <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e#comment-7040a5ae96305e884eb9>
// diff: using std::io::BufRead::fill_buf()
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}

impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}

impl<R:BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len,complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                (len, buf2[len-1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T:str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            }
            else {
                self.update_buf();
            }
        }
    }
}

pub fn continuous_division<T:PartialEq+Eq+Clone+Copy>(xs: &[T]) -> Vec<(T,usize)> {
    let mut res = vec![];
    let n = xs.len();
    for i in 0..n {
        if i == 0 || xs[i-1] != xs[i] {
            res.push((xs[i], 1));
        }
        else {
            let m = res.len();
            res[m-1].1 += 1;
        }
    }
    res
}

use std::fmt::Display;
#[allow(dead_code)]
fn write_vec<T: Display>(xs: &Vec<T>) {
    if xs.len() == 0 {
        println!();
        return;
    }
    print!("{}", xs[0]);
    for i in 1..xs.len() {
        print!(" {}", xs[i]);
    }
    println!();
}