Problem:
Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

3
2 0
3 1
1 3
4 2
0 4
5 5

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

3
0 0
1 1
5 2
2 3
3 4
4 5

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

5
0 0
7 3
2 2
4 8
1 6
8 5
6 9
5 4
9 1
3 7

Sample Output 4

5

Sample Input 5

5
0 0
1 1
5 5
6 6
7 7
2 2
3 3
4 4
8 8
9 9

Sample Output 5

4

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Maximum bipartite matching by Ford-Fulkerson
;;;
(defun find-matching (graph)
  "Takes adjacency list and returns the maximal bipartite matching. Note that
this function doesn't check if GRAPH is bipartite."
  (declare #.OPT
           ((simple-array list (*)) graph))
  (let* ((n (length graph))
         (checked (make-array n :element-type 'bit :initial-element 0))
         (matching (make-array n :element-type 'fixnum :initial-element -1))
         (res 0))
    (declare ((integer 0 #.most-positive-fixnum) res))
    (labels ((%match (vertex) ; Returns T if VERTEX is matched.
               (setf (aref checked vertex) 1)
               (dolist (candidate (aref graph vertex))
                 (let ((partner-of-candidate (aref matching candidate)))
                   (when (or (= -1 partner-of-candidate)
                             (and (zerop (aref checked partner-of-candidate))
                                  (%match partner-of-candidate)))
                     (setf (aref matching vertex) candidate
                           (aref matching candidate) vertex)
                     (return t))))))
      (dotimes (v n (values matching res))
        (when (= -1 (aref matching v))
          (fill checked 0)
          (when (%match v)
            (incf res)))))))

(defmacro split-ints-bind (vars string &body body)
  (let ((position (gensym))
        (s (gensym)))
    (labels ((expand (vars &optional init-pos)
               (if (null vars)
                   body
                   `((multiple-value-bind (,(car vars) ,position)
                         (parse-integer ,s :start ,(or init-pos position)
                                           :junk-allowed t)
                       ,@(when (null (cdr vars)) `((declare (ignore ,position))))
                       ,@(expand (cdr vars)))))))
      `(let ((,s ,string))
         (declare (string ,s))
         ,@(expand vars 0)))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body
(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (red-xs (make-array n :element-type 'uint8))
         (red-ys (make-array n :element-type 'uint8))
         (blue-xs (make-array n :element-type 'uint8))
         (blue-ys (make-array n :element-type 'uint8))
         (graph (make-array (* 2 n) :element-type 'list :initial-element nil)))
    (declare (uint8 n))
    (dotimes (idx n)
      (split-ints-bind (x y) (read-line)
        (setf (aref red-xs idx) x
              (aref red-ys idx) y)))
    (dotimes (idx n)
      (split-ints-bind (x y) (read-line)
        (setf (aref blue-xs idx) x
              (aref blue-ys idx) y)))
    (dotimes (red-idx n)
      (dotimes (blue-idx n)
        (when (and (< (aref red-xs red-idx)
                      (aref blue-xs blue-idx))
                   (< (aref red-ys red-idx)
                      (aref blue-ys blue-idx)))
          (push (+ n blue-idx) (aref graph red-idx))
          (push red-idx (aref graph (+ n blue-idx))))))
    (println (nth-value 1 (find-matching graph)))))

#-swank(main)