How to fill custom field with category ID

I am looking for a way to automatically fill a custom field with category ID (or with name).

Value of custom field should update whenever I change the category of post (or when I save post).

Thanks for the help!

enter image description here