(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

;;;
;;; Generate random trees
;;;

;; TODO: maybe better to use Prüfer sequence?

;; NOT TESTED
(defun make-random-tree (size)
  "Returns an undirected random tree of the given size."
  (declare ((integer 0 #.most-positive-fixnum) size))
  (let ((graph (make-array size :element-type 'list :initial-element nil))
        (dset (make-array size :element-type 'fixnum :initial-element -1))
        (count 0))
    (declare ((integer 0 #.most-positive-fixnum) count))
    ;; Union-Find
    (labels ((ds-root (x)
               (if (< (aref dset x) 0)
                   x
                   (setf (aref dset x)
                         (ds-root (aref dset x)))))
             (ds-unite (x1 x2)
               (let ((root1 (ds-root x1))
                     (root2 (ds-root x2)))
                 (unless (= root1 root2)
                   (when (> (aref dset root1) (aref dset root2))
                     (rotatef root1 root2))
                   (incf (aref dset root1) (aref dset root2))
                   (setf (aref dset root2) root1)))))
      (loop until (= count (- size 1))
            for u = (random size)
            for v = (random size)
            unless (= (ds-root u) (ds-root v))
            do (ds-unite u v)
               (push u (aref graph v))
               (push v (aref graph u))
               (incf count)))
    graph))

(in-package :cl-user)

(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 skolem-p (graph)
  (declare #.OPT
           ((simple-array list (*)) graph))
  (let ((n (floor (length graph) 2)))
    (declare (uint31 n))
    (labels ((dfs (v parent dest xor)
               (declare (uint31 v parent dest xor))
               (when (= v dest)
                 (if (zerop xor)
                     (return-from dfs t)
                     (return-from skolem-p nil)))
               (dolist (child (aref graph v))
                 (declare (uint31 child))
                 (unless (= child parent)
                   (when (dfs child v dest (logxor xor (+ (mod child n) 1)))
                     (return t))))))
      (dotimes (v n t)
        (dfs v (* 2 n) (+ v n) 0)))))

(defun random-search (n)
  (loop repeat 10000000000
        for graph = (make-random-tree (* 2 n))
        when (skolem-p graph)
        do (format t "~D ~D~%" (* 2 n) (- (* 2 n) 1))
           (dotimes (u (* 2 n))
                (dolist (v (aref graph u))
                  (when (< u v)
                    (format t "~D ~D~%" (+ u 1) (+ v 1)))))
           (return graph)))

(defun main ()
  (let* ((n (read))
         (out (make-string-output-stream :element-type 'base-char)))
    (labels ((no () (write-line "No") (return-from main)))
      (when (= (logcount n) 1)
        (no))
      (loop for x from 2 below (if (oddp n) n (- n 1)) by 2
            do (format out "~D ~D~%" 1 x)
               (format out "~D ~D~%" 1 (+ x 1))
               (format out "~D ~D~%" x (+ x 1 n))
               (format out "~D ~D~%" (+ x 1) (+ x n)))
      (format out "~D ~D~%" (+ n 1) (+ 3 n))
      (when (evenp n)
        (loop for x from 2 to n
              for y = (logxor n x 1)
              do (dbg x y)
              when (and (<= 2 y n) (/= x y))
              do (format out "~D ~D~%" n x)
                 (format out "~D ~D~%" (+ n n) y)
                 (return)
              finally (no)))
      (write-line "Yes")
      (write-string (get-output-stream-string out)))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (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 function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+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*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (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
       (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
"
    "Yes
1 2
2 3
3 4
4 5
5 6
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "1
"
    "No
")))


Measured memory_bytes: 29028