#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "64MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
;; -*- coding: utf-8 -*-
(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 (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(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)

(declaim (inline println))
(defun 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 ()
  (let* ((n (read))
         (points-x (make-array 100001 :element-type 'list :initial-element nil))
         (points-y (make-array 100001 :element-type 'list :initial-element nil))
         (points (make-array n :element-type '(cons uint32 uint32))))
    (dotimes (i n)
      (let ((x (read-fixnum))
            (y (read-fixnum)))
        (setf (aref points i) (cons x y))
        (push i (aref points-x x))
        (push i (aref points-y y))))
    (let ((groups (make-array n :element-type 'int32 :initial-element -1))
          (groups-xs (make-array n :element-type 'int32 :initial-element -1))
          (groups-ys (make-array n :element-type 'int32 :initial-element -1))
          (table-xs (make-hash-table))
          (table-ys (make-hash-table))
          (current 0))
      (labels ((dfs (v)
                 (setf (aref groups v) current)
                 (setf (gethash (car (aref points v)) table-xs) t)
                 (setf (gethash (cdr (aref points v)) table-ys) t)
                 (dolist (neighbor (aref points-x (car (aref points v))))
                   (when (= -1 (aref groups neighbor))
                     (dfs neighbor)))
                 (dolist (neighbor (aref points-y (cdr (aref points v))))
                   (when (= -1 (aref groups neighbor))
                     (dfs neighbor)))))
        (dotimes (i n)
          (when (= -1 (aref groups i))
            (setf table-xs (make-hash-table)
                  table-ys (make-hash-table))
            (dfs i)
            (setf (aref groups-xs current) (hash-table-count table-xs))
            (setf (aref groups-ys current) (hash-table-count table-ys))
            (incf current)))
        groups)
      (let ((group-sizes (make-array (+ 1 (reduce #'max groups))
                                     :element-type 'int32 :initial-element 0)))
        (dotimes (i n)
          (incf (aref group-sizes (aref groups i))))
        (let ((res 0))
          (dotimes (g (length group-sizes))
            (let ((num-xs (aref groups-xs g))
                  (num-ys (aref groups-ys g)))
              (incf res
                    (- (* num-xs num-ys) (aref group-sizes g)))))
          (println res))))))

#-swank(main)


;; For Test
#+swank
(defun io-equal (in-string out-string &optional (func #'main))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNC, and returns true if the
string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (and (> (length s) 0)
                      (eql (char s (- (length s) 1)) #\Linefeed))
                 s
                 (uiop:strcat s uiop:+lf+))))
    (equal (ensure-last-lf out-string)
           (with-output-to-string (out)
             (let ((*standard-output* out))
               (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                 (funcall func)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  (let ((*standard-output* out))
    (etypecase thing
      (null ; Runs #'MAIN with the string on clipboard
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname ; Runs #'MAIN with the string in a text file
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1 1
5 1
5 5
"
    "1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
10 10
20 20
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "9
1 1
2 1
3 1
4 1
5 1
1 2
1 3
1 4
1 5
"
    "16
")))


Measured memory_bytes: 58424