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

```c
#include <stdio.h>
#define dif(a,b) (((a)>(b))?(a)-(b):(b)-(a))
int main(void){
	int n,m,i,j,k,t[16],c[16],p,d;
	for( t[0] = n = i = 1; i < 16; i++ )t[i] = n *= 3;
	for(;scanf("%d%d",&n,&m),n|m;){
		for( k = 0; k < 3; k++ )
			for( scanf("%d",&i); i--; c[j] = k )scanf("%d",&j);
		p = j = 0;
		for( k = 1; k <= n; k++ ){
			d = dif( p, c[k] );
			j += t[n-k] * d;
			p = d * 2 + p & 3;
		}
		k = t[n] - 1 - j;
		if( j > k ) j = k;
		printf("%d\n", j > m ? -1 : j );
	}
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d%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
	subq	$176, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$1, -164(%rbp)
	movl	-164(%rbp), %eax
	movl	%eax, -172(%rbp)
	movl	-172(%rbp), %eax
	movl	%eax, -144(%rbp)
	jmp	.L2
.L3:
	movl	-172(%rbp), %edx
	movl	%edx, %eax
	addl	%eax, %eax
	addl	%edx, %eax
	movl	%eax, -172(%rbp)
	movl	-164(%rbp), %eax
	movl	-172(%rbp), %edx
	cltq
	movl	%edx, -144(%rbp,%rax,4)
	movl	-164(%rbp), %eax
	addl	$1, %eax
	movl	%eax, -164(%rbp)
.L2:
	movl	-164(%rbp), %eax
	cmpl	$15, %eax
	jle	.L3
	jmp	.L4
.L16:
	movl	$0, -156(%rbp)
	jmp	.L5
.L8:
	leaq	-164(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	jmp	.L6
.L7:
	leaq	-160(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-160(%rbp), %eax
	cltq
	movl	-156(%rbp), %edx
	movl	%edx, -80(%rbp,%rax,4)
.L6:
	movl	-164(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -164(%rbp)
	testl	%eax, %eax
	jne	.L7
	addl	$1, -156(%rbp)
.L5:
	cmpl	$2, -156(%rbp)
	jle	.L8
	movl	$0, -160(%rbp)
	movl	-160(%rbp), %eax
	movl	%eax, -152(%rbp)
	movl	$1, -156(%rbp)
	jmp	.L9
.L12:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	cmpl	%eax, -152(%rbp)
	jle	.L10
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %edx
	movl	-152(%rbp), %eax
	subl	%edx, %eax
	jmp	.L11
.L10:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	subl	-152(%rbp), %eax
.L11:
	movl	%eax, -148(%rbp)
	movl	-172(%rbp), %eax
	subl	-156(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	imull	-148(%rbp), %eax
	movl	%eax, %edx
	movl	-160(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -160(%rbp)
	movl	-148(%rbp), %eax
	leal	(%rax,%rax), %edx
	movl	-152(%rbp), %eax
	addl	%edx, %eax
	andl	$3, %eax
	movl	%eax, -152(%rbp)
	addl	$1, -156(%rbp)
.L9:
	movl	-172(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jle	.L12
	movl	-172(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	subl	$1, %eax
	movl	-160(%rbp), %edx
	subl	%edx, %eax
	movl	%eax, -156(%rbp)
	movl	-160(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jge	.L13
	movl	-156(%rbp), %eax
	movl	%eax, -160(%rbp)
.L13:
	movl	-160(%rbp), %edx
	movl	-168(%rbp), %eax
	cmpl	%eax, %edx
	jg	.L14
	movl	-160(%rbp), %eax
	jmp	.L15
.L14:
	movl	$-1, %eax
.L15:
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L4:
	leaq	-168(%rbp), %rdx
	leaq	-172(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-172(%rbp), %edx
	movl	-168(%rbp), %eax
	orl	%edx, %eax
	testl	%eax, %eax
	jne	.L16
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	leave
	.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
.LC4:
	.string	"%d"
.LC5:
	.string	"%d\n"
.LC6:
	.string	"%d%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB23:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	leaq	.LC4(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	movdqa	.LC0(%rip), %xmm0
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %r15
	movq	%rsp, %r14
	leaq	8(%rsp), %r13
	movaps	%xmm0, 16(%rsp)
	movdqa	.LC1(%rip), %xmm0
	leaq	12(%rsp), %r12
	movl	$14348907, (%rsp)
	movaps	%xmm0, 32(%rsp)
	movdqa	.LC2(%rip), %xmm0
	movl	$16, 8(%rsp)
	movaps	%xmm0, 48(%rsp)
	movdqa	.LC3(%rip), %xmm0
	movaps	%xmm0, 64(%rsp)
	.p2align 4,,10
	.p2align 3
.L2:
	xorl	%eax, %eax
	movq	%r15, %rdx
	leaq	.LC6(%rip), %rdi
	movq	%r14, %rsi
	call	__isoc99_scanf@PLT
	movl	(%rsp), %eax
	orl	4(%rsp), %eax
	je	.L94
	xorl	%ebx, %ebx
.L4:
	movq	%r13, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	jmp	.L92
	.p2align 4,,10
	.p2align 3
.L3:
	movq	%r12, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movslq	12(%rsp), %rax
	movl	%ebx, 80(%rsp,%rax,4)
.L92:
	movl	8(%rsp), %eax
	leal	-1(%rax), %edx
	movl	%edx, 8(%rsp)
	testl	%eax, %eax
	jne	.L3
	addl	$1, %ebx
	cmpl	$3, %ebx
	jne	.L4
	movslq	(%rsp), %rax
	movl	$0, 12(%rsp)
	testl	%eax, %eax
	jle	.L40
	movl	84(%rsp), %edx
	movl	%edx, %ecx
	negl	%ecx
	cmovs	%edx, %ecx
	leal	-1(%rax), %edx
	movslq	%edx, %rdx
	movl	16(%rsp,%rdx,4), %ebx
	imull	%ecx, %ebx
	addl	%ecx, %ecx
	andl	$3, %ecx
	movl	%ebx, %edx
	cmpl	$1, %eax
	je	.L8
	movl	88(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-2(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$2, %eax
	je	.L8
	movl	92(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%ecx, %edi
	leal	-3(%rax), %edi
	movslq	%edi, %rdi
	cmovge	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$3, %eax
	je	.L8
	movl	96(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-4(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$4, %eax
	je	.L8
	movl	100(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-5(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$5, %eax
	je	.L8
	movl	104(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-6(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$6, %eax
	je	.L8
	movl	108(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-7(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$7, %eax
	je	.L8
	movl	112(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-8(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$8, %eax
	je	.L8
	movl	116(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-9(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$9, %eax
	je	.L8
	movl	120(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-10(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$10, %eax
	je	.L8
	movl	124(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-11(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$11, %eax
	je	.L8
	movl	128(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-12(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$12, %eax
	je	.L8
	movl	132(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-13(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$13, %eax
	je	.L8
	movl	136(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-14(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$14, %eax
	je	.L8
	movl	140(%rsp), %edi
	movl	%ecx, %r8d
	subl	%edi, %r8d
	movl	%edi, %esi
	subl	%ecx, %esi
	cmpl	%ecx, %edi
	movl	%r8d, %ecx
	cmovge	%esi, %ecx
	leal	-15(%rax), %esi
	movslq	%esi, %rsi
	imull	16(%rsp,%rsi,4), %ecx
	addl	%ecx, %edx
	.p2align 4,,10
	.p2align 3
.L8:
	movl	%edx, 12(%rsp)
.L7:
	movl	%edx, %ecx
	notl	%ecx
	addl	16(%rsp,%rax,4), %ecx
	cmpl	%edx, %ecx
	jge	.L37
	movl	%ecx, 12(%rsp)
	movl	%ecx, %edx
.L37:
	cmpl	%edx, 4(%rsp)
	movl	$-1, %eax
	movl	$1, %edi
	cmovl	%eax, %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L2
.L94:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L95
	addq	$168, %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
.L40:
	.cfi_restore_state
	xorl	%edx, %edx
	jmp	.L7
.L95:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE23:
	.size	main, .-main
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.long	1
	.long	3
	.long	9
	.long	27
	.align 16
.LC1:
	.long	81
	.long	243
	.long	729
	.long	2187
	.align 16
.LC2:
	.long	6561
	.long	19683
	.long	59049
	.long	177147
	.align 16
.LC3:
	.long	531441
	.long	1594323
	.long	4782969
	.long	14348907
	.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
.LC4:
	.string	"%d"
.LC5:
	.string	"%d\n"
.LC6:
	.string	"%d%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB23:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	leaq	.LC4(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	movdqa	.LC0(%rip), %xmm0
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %r15
	movq	%rsp, %r14
	leaq	8(%rsp), %r13
	movaps	%xmm0, 16(%rsp)
	movdqa	.LC1(%rip), %xmm0
	leaq	12(%rsp), %r12
	movl	$14348907, (%rsp)
	movaps	%xmm0, 32(%rsp)
	movdqa	.LC2(%rip), %xmm0
	movl	$16, 8(%rsp)
	movaps	%xmm0, 48(%rsp)
	movdqa	.LC3(%rip), %xmm0
	movaps	%xmm0, 64(%rsp)
	.p2align 4,,10
	.p2align 3
.L2:
	xorl	%eax, %eax
	movq	%r15, %rdx
	leaq	.LC6(%rip), %rdi
	movq	%r14, %rsi
	call	__isoc99_scanf@PLT
	movl	(%rsp), %eax
	orl	4(%rsp), %eax
	je	.L94
	xorl	%ebx, %ebx
.L4:
	movq	%r13, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	jmp	.L92
	.p2align 4,,10
	.p2align 3
.L3:
	movq	%r12, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movslq	12(%rsp), %rax
	movl	%ebx, 80(%rsp,%rax,4)
.L92:
	movl	8(%rsp), %eax
	leal	-1(%rax), %edx
	movl	%edx, 8(%rsp)
	testl	%eax, %eax
	jne	.L3
	addl	$1, %ebx
	cmpl	$3, %ebx
	jne	.L4
	movslq	(%rsp), %rax
	movl	$0, 12(%rsp)
	testl	%eax, %eax
	jle	.L40
	movl	84(%rsp), %edx
	movl	%edx, %ecx
	negl	%ecx
	cmovs	%edx, %ecx
	leal	-1(%rax), %edx
	movslq	%edx, %rdx
	movl	16(%rsp,%rdx,4), %ebx
	imull	%ecx, %ebx
	addl	%ecx, %ecx
	andl	$3, %ecx
	movl	%ebx, %edx
	cmpl	$1, %eax
	je	.L8
	movl	88(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-2(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$2, %eax
	je	.L8
	movl	92(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%ecx, %edi
	leal	-3(%rax), %edi
	movslq	%edi, %rdi
	cmovge	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$3, %eax
	je	.L8
	movl	96(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-4(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$4, %eax
	je	.L8
	movl	100(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-5(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$5, %eax
	je	.L8
	movl	104(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-6(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$6, %eax
	je	.L8
	movl	108(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-7(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$7, %eax
	je	.L8
	movl	112(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-8(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$8, %eax
	je	.L8
	movl	116(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-9(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$9, %eax
	je	.L8
	movl	120(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-10(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$10, %eax
	je	.L8
	movl	124(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-11(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$11, %eax
	je	.L8
	movl	128(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-12(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$12, %eax
	je	.L8
	movl	132(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-13(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$13, %eax
	je	.L8
	movl	136(%rsp), %edi
	movl	%ecx, %esi
	movl	%edi, %r8d
	subl	%edi, %esi
	subl	%ecx, %r8d
	cmpl	%edi, %ecx
	leal	-14(%rax), %edi
	movslq	%edi, %rdi
	cmovle	%r8d, %esi
	movl	16(%rsp,%rdi,4), %ebx
	leal	(%rcx,%rsi,2), %ecx
	imull	%esi, %ebx
	andl	$3, %ecx
	addl	%ebx, %edx
	cmpl	$14, %eax
	je	.L8
	movl	140(%rsp), %edi
	movl	%ecx, %r8d
	subl	%edi, %r8d
	movl	%edi, %esi
	subl	%ecx, %esi
	cmpl	%ecx, %edi
	movl	%r8d, %ecx
	cmovge	%esi, %ecx
	leal	-15(%rax), %esi
	movslq	%esi, %rsi
	imull	16(%rsp,%rsi,4), %ecx
	addl	%ecx, %edx
	.p2align 4,,10
	.p2align 3
.L8:
	movl	%edx, 12(%rsp)
.L7:
	movl	%edx, %ecx
	notl	%ecx
	addl	16(%rsp,%rax,4), %ecx
	cmpl	%edx, %ecx
	jge	.L37
	movl	%ecx, 12(%rsp)
	movl	%ecx, %edx
.L37:
	cmpl	%edx, 4(%rsp)
	movl	$-1, %eax
	movl	$1, %edi
	cmovl	%eax, %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L2
.L94:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L95
	addq	$168, %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
.L40:
	.cfi_restore_state
	xorl	%edx, %edx
	jmp	.L7
.L95:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE23:
	.size	main, .-main
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.long	1
	.long	3
	.long	9
	.long	27
	.align 16
.LC1:
	.long	81
	.long	243
	.long	729
	.long	2187
	.align 16
.LC2:
	.long	6561
	.long	19683
	.long	59049
	.long	177147
	.align 16
.LC3:
	.long	531441
	.long	1594323
	.long	4782969
	.long	14348907
	.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:

#include <stdio.h>
#define dif(a,b) (((a)>(b))?(a)-(b):(b)-(a))
int main(void){
	int n,m,i,j,k,t[16],c[16],p,d;
	for( t[0] = n = i = 1; i < 16; i++ )t[i] = n *= 3;
	for(;scanf("%d%d",&n,&m),n|m;){
		for( k = 0; k < 3; k++ )
			for( scanf("%d",&i); i--; c[j] = k )scanf("%d",&j);
		p = j = 0;
		for( k = 1; k <= n; k++ ){
			d = dif( p, c[k] );
			j += t[n-k] * d;
			p = d * 2 + p & 3;
		}
		k = t[n] - 1 - j;
		if( j > k ) j = k;
		printf("%d\n", j > m ? -1 : j );
	}
	return 0;
}
3 10
0
1 1
2 2 3
4 20
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 3
0
1 1
2 2 3
0 0
 3 8
0
1 1
2 2 3
4 7
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 6
0
1 1
2 2 3
0 0
 3 10
0
1 1
2 2 3
4 7
2 1 2
1 2
1 4
1 5
2 1 2
0
0
3 6
0
1 1
2 2 3
0 0
 3 10
0
1 1
2 2 3
4 13
2 1 2
1 3
1 4
2 5
2 1 2
0
0
3 10
0
1 1
2 2 3
0 0
 3 10
0
1 1
2 2 3
4 7
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 3
0
1 1
2 2 3
0 0
 3 10
0
1 1
2 2 3
4 7
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 6
0
1 1
2 2 3
0 0
 3 18
0
1 1
2 2 3
4 7
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 6
0
1 1
2 2 3
0 0
 3 10
0
1 1
2 2 3
4 13
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 6
0
1 1
2 2 3
0 0
 3 18
0
1 1
2 2 3
4 20
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 3
0
1 1
2 2 3
0 0
 3 18
0
1 1
2 2 3
4 20
2 1 2
1 3
1 4
1 5
2 1 2
0
0
3 3
0
1 1
2 2 6
0 0

9
3
0
-1
 -1
3
0
-1
 9
-1
0
-1
 9
3
0
9
 9
3
0
-1
 9
3
0
-1
 9
3
0
-1
 9
3
0
-1
 9
3
0
-1
 9
3
0
-1

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

```c
#include <stdio.h>
#define dif(a,b) (((a)>(b))?(a)-(b):(b)-(a))
int main(void){
	int n,m,i,j,k,t[16],c[16],p,d;
	for( t[0] = n = i = 1; i < 16; i++ )t[i] = n *= 3;
	for(;scanf("%d%d",&n,&m),n|m;){
		for( k = 0; k < 3; k++ )
			for( scanf("%d",&i); i--; c[j] = k )scanf("%d",&j);
		p = j = 0;
		for( k = 1; k <= n; k++ ){
			d = dif( p, c[k] );
			j += t[n-k] * d;
			p = d * 2 + p & 3;
		}
		k = t[n] - 1 - j;
		if( j > k ) j = k;
		printf("%d\n", j > m ? -1 : j );
	}
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d%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
	subq	$176, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$1, -164(%rbp)
	movl	-164(%rbp), %eax
	movl	%eax, -172(%rbp)
	movl	-172(%rbp), %eax
	movl	%eax, -144(%rbp)
	jmp	.L2
.L3:
	movl	-172(%rbp), %edx
	movl	%edx, %eax
	addl	%eax, %eax
	addl	%edx, %eax
	movl	%eax, -172(%rbp)
	movl	-164(%rbp), %eax
	movl	-172(%rbp), %edx
	cltq
	movl	%edx, -144(%rbp,%rax,4)
	movl	-164(%rbp), %eax
	addl	$1, %eax
	movl	%eax, -164(%rbp)
.L2:
	movl	-164(%rbp), %eax
	cmpl	$15, %eax
	jle	.L3
	jmp	.L4
.L16:
	movl	$0, -156(%rbp)
	jmp	.L5
.L8:
	leaq	-164(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	jmp	.L6
.L7:
	leaq	-160(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-160(%rbp), %eax
	cltq
	movl	-156(%rbp), %edx
	movl	%edx, -80(%rbp,%rax,4)
.L6:
	movl	-164(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -164(%rbp)
	testl	%eax, %eax
	jne	.L7
	addl	$1, -156(%rbp)
.L5:
	cmpl	$2, -156(%rbp)
	jle	.L8
	movl	$0, -160(%rbp)
	movl	-160(%rbp), %eax
	movl	%eax, -152(%rbp)
	movl	$1, -156(%rbp)
	jmp	.L9
.L12:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	cmpl	%eax, -152(%rbp)
	jle	.L10
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %edx
	movl	-152(%rbp), %eax
	subl	%edx, %eax
	jmp	.L11
.L10:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	subl	-152(%rbp), %eax
.L11:
	movl	%eax, -148(%rbp)
	movl	-172(%rbp), %eax
	subl	-156(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	imull	-148(%rbp), %eax
	movl	%eax, %edx
	movl	-160(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -160(%rbp)
	movl	-148(%rbp), %eax
	leal	(%rax,%rax), %edx
	movl	-152(%rbp), %eax
	addl	%edx, %eax
	andl	$3, %eax
	movl	%eax, -152(%rbp)
	addl	$1, -156(%rbp)
.L9:
	movl	-172(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jle	.L12
	movl	-172(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	subl	$1, %eax
	movl	-160(%rbp), %edx
	subl	%edx, %eax
	movl	%eax, -156(%rbp)
	movl	-160(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jge	.L13
	movl	-156(%rbp), %eax
	movl	%eax, -160(%rbp)
.L13:
	movl	-160(%rbp), %edx
	movl	-168(%rbp), %eax
	cmpl	%eax, %edx
	jg	.L14
	movl	-160(%rbp), %eax
	jmp	.L15
.L14:
	movl	$-1, %eax
.L15:
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L4:
	leaq	-168(%rbp), %rdx
	leaq	-172(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-172(%rbp), %edx
	movl	-168(%rbp), %eax
	orl	%edx, %eax
	testl	%eax, %eax
	jne	.L16
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	leave
	.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
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d%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
	subq	$176, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$1, -164(%rbp)
	movl	-164(%rbp), %eax
	movl	%eax, -172(%rbp)
	movl	-172(%rbp), %eax
	movl	%eax, -144(%rbp)
	jmp	.L2
.L3:
	movl	-172(%rbp), %edx
	movl	%edx, %eax
	addl	%eax, %eax
	addl	%edx, %eax
	movl	%eax, -172(%rbp)
	movl	-164(%rbp), %eax
	movl	-172(%rbp), %edx
	cltq
	movl	%edx, -144(%rbp,%rax,4)
	movl	-164(%rbp), %eax
	addl	$1, %eax
	movl	%eax, -164(%rbp)
.L2:
	movl	-164(%rbp), %eax
	cmpl	$15, %eax
	jle	.L3
	jmp	.L4
.L16:
	movl	$0, -156(%rbp)
	jmp	.L5
.L8:
	leaq	-164(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	jmp	.L6
.L7:
	leaq	-160(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-160(%rbp), %eax
	cltq
	movl	-156(%rbp), %edx
	movl	%edx, -80(%rbp,%rax,4)
.L6:
	movl	-164(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -164(%rbp)
	testl	%eax, %eax
	jne	.L7
	addl	$1, -156(%rbp)
.L5:
	cmpl	$2, -156(%rbp)
	jle	.L8
	movl	$0, -160(%rbp)
	movl	-160(%rbp), %eax
	movl	%eax, -152(%rbp)
	movl	$1, -156(%rbp)
	jmp	.L9
.L12:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	cmpl	%eax, -152(%rbp)
	jle	.L10
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %edx
	movl	-152(%rbp), %eax
	subl	%edx, %eax
	jmp	.L11
.L10:
	movl	-156(%rbp), %eax
	cltq
	movl	-80(%rbp,%rax,4), %eax
	subl	-152(%rbp), %eax
.L11:
	movl	%eax, -148(%rbp)
	movl	-172(%rbp), %eax
	subl	-156(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	imull	-148(%rbp), %eax
	movl	%eax, %edx
	movl	-160(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -160(%rbp)
	movl	-148(%rbp), %eax
	leal	(%rax,%rax), %edx
	movl	-152(%rbp), %eax
	addl	%edx, %eax
	andl	$3, %eax
	movl	%eax, -152(%rbp)
	addl	$1, -156(%rbp)
.L9:
	movl	-172(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jle	.L12
	movl	-172(%rbp), %eax
	cltq
	movl	-144(%rbp,%rax,4), %eax
	subl	$1, %eax
	movl	-160(%rbp), %edx
	subl	%edx, %eax
	movl	%eax, -156(%rbp)
	movl	-160(%rbp), %eax
	cmpl	%eax, -156(%rbp)
	jge	.L13
	movl	-156(%rbp), %eax
	movl	%eax, -160(%rbp)
.L13:
	movl	-160(%rbp), %edx
	movl	-168(%rbp), %eax
	cmpl	%eax, %edx
	jg	.L14
	movl	-160(%rbp), %eax
	jmp	.L15
.L14:
	movl	$-1, %eax
.L15:
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L4:
	leaq	-168(%rbp), %rdx
	leaq	-172(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-172(%rbp), %edx
	movl	-168(%rbp), %eax
	orl	%edx, %eax
	testl	%eax, %eax
	jne	.L16
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	leave
	.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:
