Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
N,A[999][1000],id[1000],used[1000];
i,j;day;flag=2;
main(){
	for(scanf("%d",&N);i<N;++i)for(j=0;j<N-1;)scanf("%d",A[j++]+i);
	for(;flag&2;)
		for(flag=1,i=!++day;i<N;++i)
			used[i]<day&id[i]<N-1&&id[flag&=2,j=A[id[i]][i]-1]<N-1&&used[j]<day&A[id[j]][j]==i+1&&++id[used[i]=used[j]=day,++id[i],flag=2,j];
	printf("%d",flag*day-1);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	N
	.bss
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 3996000
A:
	.zero	3996000
	.globl	id
	.align 32
	.type	id, @object
	.size	id, 4000
id:
	.zero	4000
	.globl	used
	.align 32
	.type	used, @object
	.size	used, 4000
used:
	.zero	4000
	.globl	i
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.globl	j
	.align 4
	.type	j, @object
	.size	j, 4
j:
	.zero	4
	.globl	day
	.align 4
	.type	day, @object
	.size	day, 4
day:
	.zero	4
	.globl	flag
	.data
	.align 4
	.type	flag, @object
	.size	flag, 4
flag:
	.long	2
	.section	.rodata
.LC0:
	.string	"%d"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	N(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	jmp	.L2
.L5:
	movl	$0, j(%rip)
	jmp	.L3
.L4:
	movl	j(%rip), %eax
	leal	1(%rax), %edx
	movl	%edx, j(%rip)
	cltq
	imulq	$4000, %rax, %rax
	leaq	A(%rip), %rdx
	addq	%rax, %rdx
	movl	i(%rip), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
.L3:
	movl	N(%rip), %eax
	leal	-1(%rax), %edx
	movl	j(%rip), %eax
	cmpl	%eax, %edx
	jg	.L4
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L2:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L5
	jmp	.L6
.L11:
	movl	$1, flag(%rip)
	movl	day(%rip), %eax
	addl	$1, %eax
	movl	%eax, day(%rip)
	movl	day(%rip), %eax
	testl	%eax, %eax
	sete	%al
	movzbl	%al, %eax
	movl	%eax, i(%rip)
	jmp	.L7
.L10:
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	setl	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	flag(%rip), %eax
	andl	$2, %eax
	movl	%eax, flag(%rip)
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	subl	$1, %eax
	movl	%eax, j(%rip)
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	jge	.L9
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	j(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	addl	$1, %edx
	cmpl	%edx, %eax
	sete	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	j(%rip), %eax
	movl	day(%rip), %edx
	movslq	%eax, %rcx
	leaq	0(,%rcx,4), %rsi
	leaq	used(%rip), %rcx
	movl	%edx, (%rsi,%rcx)
	movl	i(%rip), %edx
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	used(%rip), %rax
	movl	(%rcx,%rax), %eax
	movslq	%edx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	used(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	i(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %ecx
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	$2, flag(%rip)
	movl	j(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %esi
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	%esi, (%rcx,%rdx)
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
.L9:
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L7:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L10
.L6:
	movl	flag(%rip), %eax
	andl	$2, %eax
	testl	%eax, %eax
	jne	.L11
	movl	flag(%rip), %edx
	movl	day(%rip), %eax
	imull	%edx, %eax
	subl	$1, %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
user
	.file	"temp.c"
	.text
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	N(%rip), %rsi
	xorl	%eax, %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r13, %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	scanf@PLT
	movl	i(%rip), %edx
	movl	N(%rip), %ecx
	cmpl	%edx, %ecx
	jle	.L2
	leaq	A(%rip), %rbp
	.p2align 4,,10
	.p2align 3
.L5:
	movl	$0, j(%rip)
	cmpl	$1, %ecx
	jle	.L3
	xorl	%eax, %eax
	jmp	.L6
	.p2align 4,,10
	.p2align 3
.L4:
	movl	i(%rip), %edx
.L6:
	leal	1(%rax), %ecx
	movslq	%edx, %rdx
	movq	%r13, %rdi
	imulq	$4000, %rax, %rax
	movl	%ecx, j(%rip)
	leaq	(%rax,%rdx,4), %rsi
	xorl	%eax, %eax
	addq	%rbp, %rsi
	call	scanf@PLT
	movl	N(%rip), %ecx
	movslq	j(%rip), %rax
	leal	-1(%rcx), %edx
	cmpl	%eax, %edx
	jg	.L4
	movl	i(%rip), %eax
	leal	1(%rax), %edx
	movl	%edx, i(%rip)
	cmpl	%edx, %ecx
	jg	.L5
.L2:
	movl	flag(%rip), %eax
	movb	$0, 15(%rsp)
	leal	-1(%rcx), %r12d
	leaq	id(%rip), %r9
	movl	day(%rip), %r8d
	leaq	used(%rip), %r14
	leaq	A(%rip), %r15
	testb	$2, %al
	je	.L36
	.p2align 4,,10
	.p2align 3
.L8:
	movl	%r8d, %esi
	addl	$1, %r8d
	sete	%dl
	sete	%al
	movzbl	%dl, %edx
	cmpl	%ecx, %edx
	jge	.L10
	movzbl	%al, %eax
	movl	$1, %r10d
	jmp	.L12
	.p2align 4,,10
	.p2align 3
.L38:
	imulq	$1000, %r10, %r10
	addl	$1, %r11d
	addq	%rsi, %r10
	cmpl	%r11d, (%r15,%r10,4)
	sete	%r11b
	cmpl	(%r14,%rsi,4), %r8d
	setg	%r10b
	andb	%r10b, %r11b
	movb	%r11b, 15(%rsp)
	je	.L17
	addl	$1, %edi
	movl	%r8d, (%r14,%rsi,4)
	movl	$2, %ebp
	movl	$2, %r10d
	movl	%edi, (%r9,%rax,4)
	addl	$1, (%r9,%rsi,4)
	movl	%r8d, (%r14,%rax,4)
	.p2align 4,,10
	.p2align 3
.L11:
	addq	$1, %rax
	cmpl	%eax, %ecx
	jle	.L37
.L12:
	movl	(%r9,%rax,4), %edi
	cmpl	(%r14,%rax,4), %r8d
	movl	%r10d, %ebp
	movl	%eax, %r11d
	setg	%sil
	cmpl	%r12d, %edi
	setl	%dl
	andl	$2, %ebp
	andb	%sil, %dl
	je	.L11
	movslq	%edi, %rsi
	movslq	%eax, %r10
	imulq	$1000, %rsi, %rsi
	addq	%r10, %rsi
	movl	(%r15,%rsi,4), %ebx
	subl	$1, %ebx
	movslq	%ebx, %rsi
	movslq	(%r9,%rsi,4), %r10
	cmpl	%r12d, %r10d
	jl	.L38
.L17:
	addq	$1, %rax
	movb	%dl, 15(%rsp)
	movl	%ebp, %r10d
	cmpl	%eax, %ecx
	jg	.L12
.L37:
	testl	%ebp, %ebp
	jne	.L8
	cmpb	$0, 15(%rsp)
	movl	%ecx, i(%rip)
	movl	%r8d, day(%rip)
	movl	%r10d, flag(%rip)
	je	.L13
	movl	%ebx, j(%rip)
.L13:
	movl	%r8d, %esi
	imull	%r10d, %esi
	subl	$1, %esi
	jmp	.L9
	.p2align 4,,10
	.p2align 3
.L10:
	cmpb	$0, 15(%rsp)
	movl	%edx, i(%rip)
	movl	%r8d, day(%rip)
	movl	$1, flag(%rip)
	je	.L9
	movl	%ebx, j(%rip)
.L9:
	movq	%r13, %rdi
	xorl	%eax, %eax
	call	printf@PLT
	addq	$24, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L3:
	.cfi_restore_state
	addl	$1, %edx
	movl	%edx, i(%rip)
	cmpl	%ecx, %edx
	jge	.L2
	.p2align 4,,10
	.p2align 3
.L7:
	addl	$1, %edx
	cmpl	%ecx, %edx
	jl	.L7
	movl	$0, j(%rip)
	movl	%edx, i(%rip)
	jmp	.L2
.L36:
	imull	%r8d, %eax
	leal	-1(%rax), %esi
	jmp	.L9
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.globl	flag
	.data
	.align 4
	.type	flag, @object
	.size	flag, 4
flag:
	.long	2
	.globl	day
	.bss
	.align 4
	.type	day, @object
	.size	day, 4
day:
	.zero	4
	.globl	j
	.align 4
	.type	j, @object
	.size	j, 4
j:
	.zero	4
	.globl	i
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.globl	used
	.align 32
	.type	used, @object
	.size	used, 4000
used:
	.zero	4000
	.globl	id
	.align 32
	.type	id, @object
	.size	id, 4000
id:
	.zero	4000
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 3996000
A:
	.zero	3996000
	.globl	N
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

rule
	.file	"temp.c"
	.text
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	N(%rip), %rsi
	xorl	%eax, %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r13, %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	scanf@PLT
	movl	i(%rip), %edx
	movl	N(%rip), %ecx
	cmpl	%edx, %ecx
	jle	.L2
	leaq	A(%rip), %rbp
	.p2align 4,,10
	.p2align 3
.L5:
	movl	$0, j(%rip)
	cmpl	$1, %ecx
	jle	.L3
	xorl	%eax, %eax
	jmp	.L6
	.p2align 4,,10
	.p2align 3
.L4:
	movl	i(%rip), %edx
.L6:
	leal	1(%rax), %ecx
	movslq	%edx, %rdx
	movq	%r13, %rdi
	imulq	$4000, %rax, %rax
	movl	%ecx, j(%rip)
	leaq	(%rax,%rdx,4), %rsi
	xorl	%eax, %eax
	addq	%rbp, %rsi
	call	scanf@PLT
	movl	N(%rip), %ecx
	movslq	j(%rip), %rax
	leal	-1(%rcx), %edx
	cmpl	%eax, %edx
	jg	.L4
	movl	i(%rip), %eax
	leal	1(%rax), %edx
	movl	%edx, i(%rip)
	cmpl	%edx, %ecx
	jg	.L5
.L2:
	movl	flag(%rip), %eax
	movb	$0, 15(%rsp)
	leal	-1(%rcx), %r12d
	leaq	id(%rip), %r9
	movl	day(%rip), %r8d
	leaq	used(%rip), %r14
	leaq	A(%rip), %r15
	testb	$2, %al
	je	.L36
	.p2align 4,,10
	.p2align 3
.L8:
	movl	%r8d, %esi
	addl	$1, %r8d
	sete	%dl
	sete	%al
	movzbl	%dl, %edx
	cmpl	%ecx, %edx
	jge	.L10
	movzbl	%al, %eax
	movl	$1, %r10d
	jmp	.L12
	.p2align 4,,10
	.p2align 3
.L38:
	imulq	$1000, %r10, %r10
	addl	$1, %r11d
	addq	%rsi, %r10
	cmpl	%r11d, (%r15,%r10,4)
	sete	%r11b
	cmpl	(%r14,%rsi,4), %r8d
	setg	%r10b
	andb	%r10b, %r11b
	movb	%r11b, 15(%rsp)
	je	.L17
	addl	$1, %edi
	movl	%r8d, (%r14,%rsi,4)
	movl	$2, %ebp
	movl	$2, %r10d
	movl	%edi, (%r9,%rax,4)
	addl	$1, (%r9,%rsi,4)
	movl	%r8d, (%r14,%rax,4)
	.p2align 4,,10
	.p2align 3
.L11:
	addq	$1, %rax
	cmpl	%eax, %ecx
	jle	.L37
.L12:
	movl	(%r9,%rax,4), %edi
	cmpl	(%r14,%rax,4), %r8d
	movl	%r10d, %ebp
	movl	%eax, %r11d
	setg	%sil
	cmpl	%r12d, %edi
	setl	%dl
	andl	$2, %ebp
	andb	%sil, %dl
	je	.L11
	movslq	%edi, %rsi
	movslq	%eax, %r10
	imulq	$1000, %rsi, %rsi
	addq	%r10, %rsi
	movl	(%r15,%rsi,4), %ebx
	subl	$1, %ebx
	movslq	%ebx, %rsi
	movslq	(%r9,%rsi,4), %r10
	cmpl	%r12d, %r10d
	jl	.L38
.L17:
	addq	$1, %rax
	movb	%dl, 15(%rsp)
	movl	%ebp, %r10d
	cmpl	%eax, %ecx
	jg	.L12
.L37:
	testl	%ebp, %ebp
	jne	.L8
	cmpb	$0, 15(%rsp)
	movl	%ecx, i(%rip)
	movl	%r8d, day(%rip)
	movl	%r10d, flag(%rip)
	je	.L13
	movl	%ebx, j(%rip)
.L13:
	movl	%r8d, %esi
	imull	%r10d, %esi
	subl	$1, %esi
	jmp	.L9
	.p2align 4,,10
	.p2align 3
.L10:
	cmpb	$0, 15(%rsp)
	movl	%edx, i(%rip)
	movl	%r8d, day(%rip)
	movl	$1, flag(%rip)
	je	.L9
	movl	%ebx, j(%rip)
.L9:
	movq	%r13, %rdi
	xorl	%eax, %eax
	call	printf@PLT
	addq	$24, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L3:
	.cfi_restore_state
	addl	$1, %edx
	movl	%edx, i(%rip)
	cmpl	%ecx, %edx
	jge	.L2
	.p2align 4,,10
	.p2align 3
.L7:
	addl	$1, %edx
	cmpl	%ecx, %edx
	jl	.L7
	movl	$0, j(%rip)
	movl	%edx, i(%rip)
	jmp	.L2
.L36:
	imull	%r8d, %eax
	leal	-1(%rax), %esi
	jmp	.L9
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.globl	flag
	.data
	.align 4
	.type	flag, @object
	.size	flag, 4
flag:
	.long	2
	.globl	day
	.bss
	.align 4
	.type	day, @object
	.size	day, 4
day:
	.zero	4
	.globl	j
	.align 4
	.type	j, @object
	.size	j, 4
j:
	.zero	4
	.globl	i
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.globl	used
	.align 32
	.type	used, @object
	.size	used, 4000
used:
	.zero	4000
	.globl	id
	.align 32
	.type	id, @object
	.size	id, 4000
id:
	.zero	4000
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 3996000
A:
	.zero	3996000
	.globl	N
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

N,A[999][1000],id[1000],used[1000];
i,j;day;flag=2;
main(){
	for(scanf("%d",&N);i<N;++i)for(j=0;j<N-1;)scanf("%d",A[j++]+i);
	for(;flag&2;)
		for(flag=1,i=!++day;i<N;++i)
			used[i]<day&id[i]<N-1&&id[flag&=2,j=A[id[i]][i]-1]<N-1&&used[j]<day&A[id[j]][j]==i+1&&++id[used[i]=used[j]=day,++id[i],flag=2,j];
	printf("%d",flag*day-1);
}
3
2 3
3 1
1 1
 3
2 3
3 1
2 1
 4
2 3 4
1 3 4
1 2 4
3 1 2
 4
2 3 4
1 3 4
1 4 2
3 1 2
 4
2 3 4
1 3 4
1 1 2
3 1 2
 4
2 3 4
1 3 4
1 1 4
3 1 2
 4
2 3 4
1 3 4
1 1 4
3 2 2
 4
2 3 4
1 3 4
1 2 2
3 1 2
 4
2 3 3
1 3 4
1 4 2
3 1 2
 4
2 3 3
1 3 4
1 4 2
3 1 1

-1 3 6 5 -1 -1 -1 -1 -1 -1
Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
N,A[999][1000],id[1000],used[1000];
i,j;day;flag=2;
main(){
	for(scanf("%d",&N);i<N;++i)for(j=0;j<N-1;)scanf("%d",A[j++]+i);
	for(;flag&2;)
		for(flag=1,i=!++day;i<N;++i)
			used[i]<day&id[i]<N-1&&id[flag&=2,j=A[id[i]][i]-1]<N-1&&used[j]<day&A[id[j]][j]==i+1&&++id[used[i]=used[j]=day,++id[i],flag=2,j];
	printf("%d",flag*day-1);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	N
	.bss
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 3996000
A:
	.zero	3996000
	.globl	id
	.align 32
	.type	id, @object
	.size	id, 4000
id:
	.zero	4000
	.globl	used
	.align 32
	.type	used, @object
	.size	used, 4000
used:
	.zero	4000
	.globl	i
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.globl	j
	.align 4
	.type	j, @object
	.size	j, 4
j:
	.zero	4
	.globl	day
	.align 4
	.type	day, @object
	.size	day, 4
day:
	.zero	4
	.globl	flag
	.data
	.align 4
	.type	flag, @object
	.size	flag, 4
flag:
	.long	2
	.section	.rodata
.LC0:
	.string	"%d"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	N(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	jmp	.L2
.L5:
	movl	$0, j(%rip)
	jmp	.L3
.L4:
	movl	j(%rip), %eax
	leal	1(%rax), %edx
	movl	%edx, j(%rip)
	cltq
	imulq	$4000, %rax, %rax
	leaq	A(%rip), %rdx
	addq	%rax, %rdx
	movl	i(%rip), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
.L3:
	movl	N(%rip), %eax
	leal	-1(%rax), %edx
	movl	j(%rip), %eax
	cmpl	%eax, %edx
	jg	.L4
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L2:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L5
	jmp	.L6
.L11:
	movl	$1, flag(%rip)
	movl	day(%rip), %eax
	addl	$1, %eax
	movl	%eax, day(%rip)
	movl	day(%rip), %eax
	testl	%eax, %eax
	sete	%al
	movzbl	%al, %eax
	movl	%eax, i(%rip)
	jmp	.L7
.L10:
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	setl	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	flag(%rip), %eax
	andl	$2, %eax
	movl	%eax, flag(%rip)
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	subl	$1, %eax
	movl	%eax, j(%rip)
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	jge	.L9
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	j(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	addl	$1, %edx
	cmpl	%edx, %eax
	sete	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	j(%rip), %eax
	movl	day(%rip), %edx
	movslq	%eax, %rcx
	leaq	0(,%rcx,4), %rsi
	leaq	used(%rip), %rcx
	movl	%edx, (%rsi,%rcx)
	movl	i(%rip), %edx
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	used(%rip), %rax
	movl	(%rcx,%rax), %eax
	movslq	%edx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	used(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	i(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %ecx
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	$2, flag(%rip)
	movl	j(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %esi
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	%esi, (%rcx,%rdx)
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
.L9:
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L7:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L10
.L6:
	movl	flag(%rip), %eax
	andl	$2, %eax
	testl	%eax, %eax
	jne	.L11
	movl	flag(%rip), %edx
	movl	day(%rip), %eax
	imull	%edx, %eax
	subl	$1, %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
train
	.file	"temp.c"
	.text
	.globl	N
	.bss
	.align 4
	.type	N, @object
	.size	N, 4
N:
	.zero	4
	.globl	A
	.align 32
	.type	A, @object
	.size	A, 3996000
A:
	.zero	3996000
	.globl	id
	.align 32
	.type	id, @object
	.size	id, 4000
id:
	.zero	4000
	.globl	used
	.align 32
	.type	used, @object
	.size	used, 4000
used:
	.zero	4000
	.globl	i
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.globl	j
	.align 4
	.type	j, @object
	.size	j, 4
j:
	.zero	4
	.globl	day
	.align 4
	.type	day, @object
	.size	day, 4
day:
	.zero	4
	.globl	flag
	.data
	.align 4
	.type	flag, @object
	.size	flag, 4
flag:
	.long	2
	.section	.rodata
.LC0:
	.string	"%d"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	N(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	jmp	.L2
.L5:
	movl	$0, j(%rip)
	jmp	.L3
.L4:
	movl	j(%rip), %eax
	leal	1(%rax), %edx
	movl	%edx, j(%rip)
	cltq
	imulq	$4000, %rax, %rax
	leaq	A(%rip), %rdx
	addq	%rax, %rdx
	movl	i(%rip), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
.L3:
	movl	N(%rip), %eax
	leal	-1(%rax), %edx
	movl	j(%rip), %eax
	cmpl	%eax, %edx
	jg	.L4
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L2:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L5
	jmp	.L6
.L11:
	movl	$1, flag(%rip)
	movl	day(%rip), %eax
	addl	$1, %eax
	movl	%eax, day(%rip)
	movl	day(%rip), %eax
	testl	%eax, %eax
	sete	%al
	movzbl	%al, %eax
	movl	%eax, i(%rip)
	jmp	.L7
.L10:
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	setl	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	flag(%rip), %eax
	andl	$2, %eax
	movl	%eax, flag(%rip)
	movl	i(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	subl	$1, %eax
	movl	%eax, j(%rip)
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	N(%rip), %edx
	subl	$1, %edx
	cmpl	%edx, %eax
	jge	.L9
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	used(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	day(%rip), %eax
	cmpl	%eax, %edx
	setl	%cl
	movl	j(%rip), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	j(%rip), %edx
	movslq	%edx, %rdx
	cltq
	imulq	$1000, %rax, %rax
	addq	%rdx, %rax
	leaq	0(,%rax,4), %rdx
	leaq	A(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	i(%rip), %edx
	addl	$1, %edx
	cmpl	%edx, %eax
	sete	%al
	andl	%ecx, %eax
	testb	%al, %al
	je	.L9
	movl	j(%rip), %eax
	movl	day(%rip), %edx
	movslq	%eax, %rcx
	leaq	0(,%rcx,4), %rsi
	leaq	used(%rip), %rcx
	movl	%edx, (%rsi,%rcx)
	movl	i(%rip), %edx
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	used(%rip), %rax
	movl	(%rcx,%rax), %eax
	movslq	%edx, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	used(%rip), %rdx
	movl	%eax, (%rcx,%rdx)
	movl	i(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %ecx
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	$2, flag(%rip)
	movl	j(%rip), %eax
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	(%rcx,%rdx), %edx
	leal	1(%rdx), %esi
	movslq	%eax, %rdx
	leaq	0(,%rdx,4), %rcx
	leaq	id(%rip), %rdx
	movl	%esi, (%rcx,%rdx)
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	id(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
.L9:
	movl	i(%rip), %eax
	addl	$1, %eax
	movl	%eax, i(%rip)
.L7:
	movl	i(%rip), %edx
	movl	N(%rip), %eax
	cmpl	%eax, %edx
	jl	.L10
.L6:
	movl	flag(%rip), %eax
	andl	$2, %eax
	testl	%eax, %eax
	jne	.L11
	movl	flag(%rip), %edx
	movl	day(%rip), %eax
	imull	%edx, %eax
	subl	$1, %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
